弹簧自动布线发生在@BeforeClass后,当使用maven-surefire运行测试时
2022-09-02 00:44:36
我在依赖注入(Spring autowiring)和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].
我该如何解决这个问题?如果我用它替换,它可以在maven中工作,就像在TestNG的eclipse插件中一样。@BeforeClass
@Test
maven-surefire-plugin:2.7.2
Eclipse: Helios Service Release 1
jdk1.6.0_14
测试NG: 5.14.10