我在依赖注入(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].
我怎么解决这个问题?
如果我更换@BeforeClass
with @Test
它在 Maven 中的工作方式与 TestNG 的 eclipse 插件中一样。
maven-surefire-插件:2.7.2
Eclipse:Helios 服务版本 1
jdk1.6.0_14
测试NG:5.14.10
另外,直到这个问题 https://jira.springsource.org/browse/SPR-4072已修复,如果在遵循前面的建议后仍然无法正常工作,或者您不希望在每个方法之前执行代码,则将以下代码添加到您的测试类中:
@Override
@BeforeSuite
protected void springTestContextPrepareTestInstance() throws Exception {
super.springTestContextPrepareTestInstance();
}
这可以确保 Spring Context 在执行 @BeforeClass 方法之前准备好。
*注意,我发布了这个答案,因为在标题中您询问了 @BeforeClass,即使您的示例代码中没有使用 @BeforeClass。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)