【发布时间】:2017-03-23 22:53:44
【问题描述】:
我正在运行一个使用 TestNG 作为测试框架的 Spring Boot 应用程序。我的测试是这样设置的:
一个父类,负责设置逻辑并负责所有的配置工作:
@ContextConfiguration(classes = {TestingConfig.class}, initializers = ConfigFileApplicationContextInitializer.class)
@ContextConfiguration(classes = TestConfig.class)
@TestPropertySource(locations = "classpath:application.yml")
public abstract ParentTestClass extends AbstractTestNGSpringContextTests {
@Autowired
private ServiceClient serviceClient;
@BeforeSuite
public void beforeClass() {
Assert.assertNotNull(serviceClient);
serviceClient.doSomeSetupWork();
}
}
有多个子测试类。每个 on 都继承自父测试类,因此它们共享相同的设置逻辑。
public ChildTestClass1 extends ParentTestClass {
@Test
public void someTest() {
...
}
// More tests not shown
}
public ChildTestClass2 extends ParentTestClass {
@Test
public void anotherTest() {
...
}
// More tests not shown
}
serviceClient 是测试套件所依赖的 Web 服务之一的客户端。在运行测试用例之前,我正在与服务客户端进行调用以在其他服务中设置数据。
问题是这样的:以前我使用@BeforeClass 注释,这意味着父类的 setup 方法为每个子测试类运行一次。这没问题,但是等待多次运行相同的设置真的很慢。
所以我心想:我只需将 ParentTestClass 中的 @BeforeClass 注释更改为 @BeforeSuite!这将解决我所有的问题!
错了。
现在当我运行它时,父类的beforeClass() 方法中的Assert.assertNotNull(serviceClient); 行失败。 简而言之,Spring 依赖项并没有被注入到 @BeforeSuite 注释的方法中,即使它们被注入到使用 @BeforeClass 注释的方法中。
这里有什么想法吗?我真的很感激!
【问题讨论】:
标签: java spring unit-testing testng