Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 321
KTrum Avatar asked May 25 '26 08:05

KTrum


1 Answers

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.
  }
}
like image 95
Stephane Nicoll Avatar answered May 27 '26 20:05

Stephane Nicoll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!