Home > AI > Backend > SpringBoot > Mockito >

Mockito Intro

What can we test and verify with MockMvc?

That’s easy to answer: Everything related to Spring MVC!

This includes your controller endpoints (both @Controller and @RestController) and any Spring MVC infrastructure components like Filter@ControllerAdviceWebMvcConfigurer, etc.

Possible test scenarios can be the following:

  • Does my REST API controller return a proper JSON response?
  • Is the model for my Thymeleaf view properly initialized?
  • Does my endpoint return the HTTP status code 418 if my service layer throws a TeaNotFoundException?
  • Is my REST API endpoint properly protected with Basic Auth?
  • Can only users with the role ADMIN access the DELETE endpoint?
  • etc.

With MockMvc we perform requests against a mocked servlet environment. There won’t be any real HTTP communication during our tests. We rather directly work with the mocked environment provided by Spring.  MockMvc acts as the entry-point to this mocked servlet environment. Similar to the WebTestClient when accessing our started Servlet container over HTTP.

Leave a Reply