Testing Spring Boot Applications
A Spring Boot application is a Spring ApplicationContext
, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.
|
External properties, logging, and other features of Spring Boot are installed in the context by default only if you use SpringApplication to create it.
|
|
If you are using JUnit 4, do not forget to also add @RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored.
If you are using JUnit 5, there is no need to add the equivalent @ExtendWith(SpringExtension.class) as @SpringBootTest and the other @…Test annotations are already annotated with it.
|
By default, @SpringBootTest
will not start a server.
You can use the webEnvironment
attribute of @SpringBootTest
to further refine how your tests run:
-
MOCK
(Default) : Loads a web ApplicationContext
and provides a mock web environment.
Embedded servers are not started when using this annotation.
If a web environment is not available on your classpath, this mode transparently falls back to creating a regular non-web ApplicationContext
.
It can be used in conjunction with @AutoConfigureMockMvc
or @AutoConfigureWebTestClient
for mock-based testing of your web application.
-
RANDOM_PORT
: Loads a WebServerApplicationContext
and provides a real web environment.
Embedded servers are started and listen on a random port.
-
DEFINED_PORT
: Loads a WebServerApplicationContext
and provides a real web environment.
Embedded servers are started and listen on a defined port (from your application.properties
) or on the default port of 8080
.
-
NONE
: Loads an ApplicationContext
by using SpringApplication
but does not provide any web environment (mock or otherwise).
|
If your test is @Transactional , it rolls back the transaction at the end of each test method by default.
However, as using this arrangement with either RANDOM_PORT or DEFINED_PORT implicitly provides a real servlet environment, the HTTP client and server run in separate threads and, thus, in separate transactions.
Any transaction initiated on the server does not roll back in this case.
|
|
@SpringBootTest with webEnvironment = WebEnvironment.RANDOM_PORT will also start the management server on a separate random port if your application uses a different port for the management server.
|