[Index of Spring blogs]
Use “spring-boot-starter-test” to add all dependencies:
org.springframework.boot
spring-boot-starter-test
test
You can annotate the test class with :
- (nothing)
- @SpringBootTest(webEnvironment = MOCK / RANDOM_PORT / DEFINED_PORT)
- @AutoConfigureMockMvc
- @WebMvcTest
To make rest call’s to the controllers, you can use one of the following:
- RestTemplate
- TestRestTemplate
- RestAssuredMockMvc
- MockMvc
- WebTestClient (only when using webflux)
@SpringBootTest will load your complete Spring application, but without the Servlet container when specifying (webEnvironment = MOCK).
When using webEnvironment = RANDOM_PORT, a random port is used (which you can find in the test using @LocalServerPort).
It is also possible to autowire a TestRestTemplate, RestAssuredMockMvc or MockMvc which you can use to test the application using relative url’s.
@MockBean can be used to mock any bean in the application environment.
To test the serialization and deserialization of a JSON object, you can use @JsonTest
To test a web client, you can use @RestClientTest
Examples of using the different annotations and rest clients can be found below:
Lees meer »
Je moet ingelogd zijn om een reactie te plaatsen.