@beforeclassを実行するには、前の春のテストコンテキストがロードアップするにはどうすればよいですか?
-
14-10-2019 - |
質問
Testngを使用してプログラマーにとってはケーキでなければなりません。このシナリオがあります
@ContextConfiguration(locations={"customer-form-portlet.xml", "classpath:META-INF2/base-spring.xml" })
public class BaseTestCase extends AbstractTestNGSpringContextTests {
...
@BeforeClass
public void setUpClass() throws Exception {
しかし、@beforeclassの後にスプリングコンテキストをロードする必要があります。 IIは、オーバーライドAbstractTestNgspringContextTestsメソッドを思いつきました。
@BeforeClass(alwaysRun = true)
protected void springTestContextBeforeTestClass() throws Exception {
this.testContextManager.beforeTestClass();
}
@BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")
protected void springTestContextPrepareTestInstance() throws Exception {
this.testContextManager.prepareTestInstance(this);
}
そして私の方法を作ります
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
protected void springTestContextPrepareTestClass() throws Exception {
}
しかし、それから私は得ます:
原因:原因:org.testngexception:org.springframework.test.context.testng.abstracttestngspringcontexttests.springtestcontextpreparetestinstance()は、保護されている声に依存することは許可されていません。 lang.exception
それを公開することも役に立ちません。テストコンテキストを手動でロードできることを知っているが、それはそれほど空想的ではないことを知っている:-)誰でもここで言及することができます。
このように機能しますが、testContextManagerが表示されないため、preateTestinStance()メソッドを呼び出すことはできません。
@Override
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
public void springTestContextPrepareTestInstance() throws Exception {
}
解決
さて、私はカスタム依存関係のあるdectuneTestExecutionListenerを作成しました、そして私はOverriden InjectDependencies()メソッドを持っています、そしてそこに私のinitコードを実行しました
@TestExecutionListeners( inheritListeners = false, listeners = {DITestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@ContextConfiguration(locations= "customer-form-portlet.xml")
public class BaseTestCase extends AbstractTestNGSpringContextTests {
と
public class DITestExecutionListener extends DependencyInjectionTestExecutionListener {
protected void injectDependencies(final TestContext testContext) throws Exception {
INITSTUFF();
Object bean = testContext.getTestInstance();
AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
beanFactory.initializeBean(bean, testContext.getTestClass().getName());
testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}
所属していません StackOverflow