Oliver
Oliver

Reputation: 433

Start and shutdown Spring application context in a unit test

What's the recommended way to start and gracefully shutdown a Spring application within a unit test? I am aware of the SpringExtension for JUnit5, but that starts and stops the Spring context before/after the test method execution.

I would like to start and stop the spring context withing the test method as I would like to verify that certain methods are called (especially during shutdown).

Is there a recommended way how to achieve that?

Upvotes: 2

Views: 1539

Answers (1)

Sam Brannen
Sam Brannen

Reputation: 31197

If you want to test shutdown callbacks, you're probably better of creating and shutting down the ApplicationContext directly within your tests instead of letting the Spring TestContext Framework manage the context for you.

You might also find the AssertJ support in Spring Boot Test useful.

Upvotes: 1

Related Questions