List of Spring Boot Starter Dependencies

[Index of Spring blogs]

Core:

Lees meer »

Testing the Spring MVC layer

[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 »

Spring WebFlux samples and links

[Index of Spring blogs]

Samples:
https://github.com/robbertvdzon/springfluxsamples/blob/master/src/main/java/com/example/springwebflux/SpringwebfluxApplication.java

Spring5 documentation about webFlux:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux

Spring Boot documentation about webFlux:
https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-webflux

Spring testing documentation about Spring Web Reactive:
https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#mock-objects-web-reactive

 

Spring WebMVC samples and links

[Index of Spring blogs]

Samples:
https://github.com/robbertvdzon/spring-web-mvc-samples/blob/master/src/main/java/com/example/demo/DemoApplication.java

Spring5 documentation about webMVC:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html

Spring Boot documentation about webMVC:
https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications

Spring testing documentation about MVC Test Framework:
https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-framework