Search code examples
springspring-bootspring-testqpid

SpringBootTest: how to know when boot application is done


Spring boot integration test looks like this

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application)
class IntegrationTest {

  static QpidRunner qpidRunner

  @BeforeClass
  static void init() {
    qpidRunner = new QpidRunner()
    qpidRunner.start()
  }

  @AfterClass
  static void tearDown() {
    qpidRunner.stop()
  }

}

So, Qpid instance is run before and teared down after all tests. I want to know is there a way to check whether spring boot application is still running before calling qpidRunner.stop(). I want to stop Qpid only when I'm sure that spring app has finished its stopping.


Solution

  • Taking into account that ConfigurableWebApplicationContext can be injected in a SpringBootTest, adding this lines to the code solved the problem

    static ConfigurableWebApplicationContext context
    
    @Autowired
    void setContext(ConfigurableWebApplicationContext context) {
      AbstractDocsIntegrationTest.context = context
    }
    
    @AfterClass
    static void tearDown() {
      context.stop()
      qpidRunner.stop()
    }
    

    Spring docs about stop method

    Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

    JUnit AfterClass annotated method must be static, therefore @Autowired workaround with setContext method.