KTrum KTrum - 2 months ago 22
Java Question

Spring boot test - wait for termination

I have an application written with Spring boot and I am trying to write some integration tests for it. I want to run my spring boot application and wait until it terminates so I can assert some state.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApp.class)
@IntegrationTest("initParams")
public class Test {
@Test
public void test() {
// need to wait until termination
}


Is there any way I can wait for the termination of the Spring app without modifying the application code?

Answer

If you the test needs the app to terminates, it would be better to actually run the app as part of the test rather than letting the test context starts it for you. I am not sure why you need it and I wouldn't recommend it for all use cases but this could work:

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.
  }
}
Comments