使用maven-surefire运行测试时,在@BeforeClass之后发生Spring-Autowiring [英] Spring-Autowiring happens after @BeforeClass when running test with maven-surefire

查看:113
本文介绍了使用maven-surefire运行测试时,在@BeforeClass之后发生Spring-Autowiring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在依赖项注入(Spring自动装配)和maven-surefire方面存在一些问题. 当在带有TestNG的Eclipse中运行时,以下测试可以正常工作: 注入服务对象,然后调用@BeforeClass方法.

@TransactionConfiguration(defaultRollback=false)
@ContextConfiguration(locations={"/testContext.xml"})
public class MyServiceTest extends AbstractTransactionalTestNGSpringContextTests {


@Autowired
private MyService service;

@BeforeTest
public void setup() {
    System.out.println("*********************"+service);
    Assert.assertNotNull(service);
}

但是,当我使用maven-surefire运行相同的测试用例时,将调用first setup(),这将导致测试失败:

[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ myserver ---
[INFO] Surefire report directory: D:\...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
**************************null
2011-03-04 11:08:57,462 DEBUG  ionTestExecutionListener.prepareTestInstance  - Performing dependency injection for test context [[TestContext@1fd6bea...
2011-03-04 11:08:57,462 DEBUG  ractGenericContextLoader.loadContext          - Loading ApplicationContext for locations [classpath:/testContext.xml].

我该如何解决这个问题? 如果我将@BeforeClass替换为@Test,则它可以在Maven中工作,就像在TestNG的eclipse插件中一样.

maven-surefire-plugin:2.7.2

Eclipse:Helios Service Release 1

jdk1.6.0_14

TestNG:5.14.10

解决方案

此外,直到解决了此问题 ,如果在遵循了先前的建议后一切仍然无法满足您的要求,或者您不希望在每个方法之前都执行代码,则将以下代码添加到测试类中:

@Override
@BeforeSuite
protected void springTestContextPrepareTestInstance() throws Exception {
    super.springTestContextPrepareTestInstance();
}

这可以确保在执行@BeforeClass方法之前就可以准备Spring Context.

*请注意,即使您的示例代码中没有使用@BeforeClass,我还是在标题中询问有关@BeforeClass的问题,以发布此答案.

I have some problems with dependency injection (Spring autowiring) and maven-surefire. The following test works without problems when run in eclipse with TestNG: The service-object is injected, then the @BeforeClass-method is called.

@TransactionConfiguration(defaultRollback=false)
@ContextConfiguration(locations={"/testContext.xml"})
public class MyServiceTest extends AbstractTransactionalTestNGSpringContextTests {


@Autowired
private MyService service;

@BeforeTest
public void setup() {
    System.out.println("*********************"+service);
    Assert.assertNotNull(service);
}

However, when I run the very same testcase with maven-surefire, first setup() is called, which causes the test to fail:

[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ myserver ---
[INFO] Surefire report directory: D:\...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
**************************null
2011-03-04 11:08:57,462 DEBUG  ionTestExecutionListener.prepareTestInstance  - Performing dependency injection for test context [[TestContext@1fd6bea...
2011-03-04 11:08:57,462 DEBUG  ractGenericContextLoader.loadContext          - Loading ApplicationContext for locations [classpath:/testContext.xml].

How can I solve this problem? If I replace @BeforeClass with @Test it works in maven as in TestNG's eclipse plugin.

maven-surefire-plugin:2.7.2

Eclipse: Helios Service Release 1

jdk1.6.0_14

TestNG: 5.14.10

解决方案

Additionally, until this issue is fixed, if things still aren't working for you after following the previous advice OR you do not wish your code to be executed before every single method, then add the following code to your test class:

@Override
@BeforeSuite
protected void springTestContextPrepareTestInstance() throws Exception {
    super.springTestContextPrepareTestInstance();
}

This ensures that the Spring Context will be prepared before your @BeforeClass methods are executed.

*note, I posted this answer since in the title you're asking about @BeforeClass, even though there is no usage of @BeforeClass in your sample code.

这篇关于使用maven-surefire运行测试时,在@BeforeClass之后发生Spring-Autowiring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆