使用 maven-surefire 运行测试时,Spring-Autowiring 在 @BeforeClass 之后发生
我在依赖注入(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 运行相同的测试用例时,首先调用 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].
我该如何解决这个问题? 如果我用 @Test
替换 @BeforeClass
,它可以在 Maven 中工作,就像在 TestNG 的 Eclipse 插件中一样。
maven-surefire-plugin:2.7.2
Eclipse:Helios 服务版本 1
jdk1.6.0_14
TestNG:5.14.10
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此外,在此问题得到解决之前,如果按照之前的建议后仍然无法正常工作,或者您如果您不希望您的代码在每个方法之前执行,那么请将以下代码添加到您的测试类中:
这可确保在执行您的 @BeforeClass 方法之前准备好 Spring Context。
*注意,我发布了这个答案,因为在标题中您询问了 @BeforeClass,即使您的示例代码中没有使用 @BeforeClass。
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:
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.
使用@BeforeMethod,而不是@BeforeTest。
Use
@BeforeMethod
, not@BeforeTest
.我同意 Cedric 的观点:使用
@BeforeMethod
而不是@BeforeTest
,因为 Spring 的依赖注入发生在@BeforeClass
方法中。I agree with Cedric: use
@BeforeMethod
instead of@BeforeTest
, since Spring's dependency injection occurs in an@BeforeClass
method.使用@PostConstruct而不是@BeforeXXX
Use @PostConstruct not @BeforeXXX
检查您是否也有 spring-asm 依赖项。如果你有一个它将与 spring-core 依赖冲突。我删除了 asm 依赖项,这对我有用。
Check if you have spring-asm dependency as well. If you have one it will conflict with spring-core dependency. I removed the asm dependency and this worked for me.