我在依赖注入(Spring 自动装配)和 maven-surefire 方面遇到了一些问题。以下测试在 Eclipse 中使用 TestNG 运行时没有问题: 注入服务对象,然后@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
它,@Test
它可以在 Maven 中工作,就像在 TestNG 的 eclipse 插件中一样。
maven-surefire-插件:2.7.2
Eclipse:Helios 服务版本 1
jdk1.6.0_14
测试NG:5.14.10