5

これは、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.testng.TestNGException: org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() は、保護された void org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestClass() に依存することは許可されていません。 lang.Exception

公開することも役に立ちません。動作する方法で実行できる場合は、ここで誰か言及してください:-) testContext を手動でロードできることは知っていますが、それほど派手ではありません。

このように動作しますが、 TestContextManager が表示されないため、 prepareTestInstance() メソッドを呼び出すことができません:

@Override
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
public void springTestContextPrepareTestInstance() throws Exception {
}
4

1 に答える 1

2

カスタム DependencyInjectionTestExecutionListener を作成し、 injectDependencies() メソッドをオーバーライドして、そこで初期化コードを実行しました

@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);
    }
于 2011-01-04T00:02:22.580 に答える