Spring Boot is a great gift for all Spring developers. The productivity boost is enormous. It also makes it extremely easy to unit tests slices of your layered application. This feature is available from Spring Boot 1.4.x onwards. In this post, I am going to focus only on unit testing of Spring MVC classic controller i.e non-REST controller.
Spring Boot test slice support for MVC controller is enabled by the following annotation
This tells Spring MVC test framework that we only intend to test
In the example above, we are testing the request to copy the Employee. Mockito is used to create and inject the mock objects. Spring takes care of the mock objects, dependencies for us. This makes it extremely easy to write unit tests for classic Spring MVC controllers. Note that for quick feedback unit tests must be cheap i.e quick to develop, setup and execute. This goal is achieved very easily with this setup.
Beware
However, the Spring MVC controller test has one drawback. In this scenario, you would want to just test the functionality of the controller method by actually simulating the request/response cycle. This is done perfectly by Spring MVC test framework. However, it also loads/generates the view. In other words, it takes the logical view name from
Github Repo :
https://github.com/kdhrubo/playground
Spring Boot test slice support for MVC controller is enabled by the following annotation
This tells Spring MVC test framework that we only intend to test
EmployeeController
So it will only make the web MVC components (controllers, interceptors etc) available with mocking support.
For more details of this annotation please refer to the Javadoc here.In the example above, we are testing the request to copy the Employee. Mockito is used to create and inject the mock objects. Spring takes care of the mock objects, dependencies for us. This makes it extremely easy to write unit tests for classic Spring MVC controllers. Note that for quick feedback unit tests must be cheap i.e quick to develop, setup and execute. This goal is achieved very easily with this setup.
Beware
However, the Spring MVC controller test has one drawback. In this scenario, you would want to just test the functionality of the controller method by actually simulating the request/response cycle. This is done perfectly by Spring MVC test framework. However, it also loads/generates the view. In other words, it takes the logical view name from
ModelAndView
object generates HTML content. This can potentially slow down the unit test execution time. This is also redundant as you will not be interested in testing the content of the HTML when testing for a controller (this can be done by Thyemeleaf test framework). In order to circumvent this limitation, I have added a blank test Thymeleaf template.
Given this limitation, it will be very nice if Spring provides a switch/configuration to turn off the view generation.
Github repository for this project is available atGithub Repo :
https://github.com/kdhrubo/playground
Comments
Post a Comment