There are different ways to test your Controller (Web or API Layer) classes in Spring Boot, some provide support to write pure Unit Tests and some others are more intended to be used for Integration Tests. Within this post, I’ll cover the main three test approaches available for Spring: using MockMVC in standalone mode, MockMVC together with SpringRunner, and using SpringBootTest.
Within this post, I show you how to setup properly a Unit Test in Spring Boot performing HTTP requests with Mockito. In this case, I’m testing directly against data to be placed into the model, but a similar test could be written for checking a REST API.
Sometimes testing web access with Spring Boot can be tricky. There are some specific annotations to be used, being the configuration to use maybe the most important one.