如何使 @BeforeClass 在 Spring TestContext 加载之前运行?

发布于 2024-10-10 04:42:22 字数 1657 浏览 5 评论 0原文

对于使用 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 之后加载 spring 上下文。我想出了重写 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() 不允许依赖受保护的 空白 org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestClass() 抛出 java.lang.Exception

将其公开也没有帮助。如果可以以工作方式完成,请有人在这里提及:-) 我知道我可以手动加载 testContext,但这不会那么花哨。

它的工作原理如下,但 TestContextManager 不可见,因此我无法对其调用prepareTestInstance() 方法:

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

it should be piece of cake for programmers using testNG. I have this scenario

    @ContextConfiguration(locations={"customer-form-portlet.xml", "classpath:META-INF2/base-spring.xml" })
    public class BaseTestCase extends AbstractTestNGSpringContextTests {

...
        @BeforeClass
        public void setUpClass() throws Exception {

But I'd need the spring context to be load up after @BeforeClass. I I came up with overriding AbstractTestNGSpringContextTests methods :

@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);
}

and make my method

@BeforeClass(alwaysRun = true, dependsOnMethods = "setUpClass")
protected void springTestContextPrepareTestClass() throws Exception {
}

But then I get :

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

Make it public also doesn't help. Could please anybody mention here if it can be done in a working manner :-) I know that I could load the testContext manually, but that wouldn't be so fancy.

It works like this, but TestContextManager is not visible so I can't call prepareTestInstance() method on it :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-10-17 04:42:22

好吧,我创建了自定义 DependencyInjectionTestExecutionListener,并且我已经重写了jectDependency() 方法并在那里完成了我的初始化代码

@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);
    }

Well I created custom DependencyInjectionTestExecutionListener and I have overriden injectDependencies() method and done my init code in there

@TestExecutionListeners( inheritListeners = false, listeners = {DITestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@ContextConfiguration(locations= "customer-form-portlet.xml")
public class BaseTestCase extends AbstractTestNGSpringContextTests {

AND

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文