对于使用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提出了覆盖的摘要testngspringcontexttests方法:

@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 {
}

但是后来我得到了:

Caused by: org.testng.TestNGException: org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() is not allowed to depend on protected void org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestClass() throws java. lang.Exception

公开它也无济于事。如果可以以工作方式完成任何人,请在这里取消任何提及的人:-)我知道我可以手动加载testContext,但这并不是那么花哨。

它是这样的工作,但是TestContextManager不可见,因此我无法将其调用preparetestinstance()方法:

@Override
@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
public void springTestContextPrepareTestInstance() throws Exception {
}
有帮助吗?

解决方案

好吧,我创建了自定义依赖项testexecutionlistener,并且我有Overriden注射依赖性()方法,并在其中完成了我的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);
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top