我有一个用 Spring boot 编写的应用程序,我正在尝试为其编写一些集成测试。我想运行我的 spring boot 应用程序并等待它终止,这样我就可以断言一些状态。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@IntegrationTest("initParams")
public class Test {
@Test
public void test() {
// need to wait until termination
}
有什么方法可以在不修改应用程序代码的情况下等待 Spring 应用程序终止?
最佳答案
如果您的测试需要应用程序终止,最好将应用程序实际作为测试的一部分运行,而不是让测试上下文为您启动它。我不确定你为什么需要它,我不会推荐它用于所有用例,但这可能有效:
public class Test {
@Test
public void test() {
String[] args = new String[]{"initParams"}; // To adapt
ConfigurableApplicationContext ctx = SpringApplication.run(MyApp.class, args);
// whatever
ctx.close();
// whatever after the app has terminated.
}
}
关于java - Spring Boot 测试——等待终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39208636/