I have recently blogged about @DataJpaTest annotation from Spring Boot v1.4. It is one of test improvements introduced in this version. The other one is @WebMvcTest which I would like to write about today.

What is its purpose? This annotation automatically configures tests of web endpoints provided by our application. It narrows the number of classes loaded to the ones required to have a complete MVC test environment.

Here is briefly what does it do under the hood:

  • bootstraps test context by @BootstrapWith(WebMvcTestContextBootstrapper.class),
  • imports cache configuration (@AutoConfigureCache) as @DataJpaTest does,
  • imports configuration of web MVC (handlers for incoming requests, raised exceptions and web resources, view controllers, converters of responses send back, etc.) through AutoConfigureWebMvc,
  • configures mock MVC with AutoConfigureMockMvc which allows setting whether security support should be enabled, web driver (for Selenium) and client (for HtmlUnit) should be used for tests and how to proceed with printing of received MVC results,
  • imports controllers specified as a parameter value of WebMvcTest.

Comparing to the previous way of writing MVC tests, @WebMvcTest replaces two annotations: @WebAppConfiguration and @ContextConfiguration (in case of using Spock in tests the latter has still to be provided). With possibility to only load controller classes provided in the annotation parameter, tests become more lightweight.

I was a bit surprised that MvcResult is printed every time, even I do not use MockMvcResultHandlers.print() handler after performing a test call. It turned out the printing is enabled by default. This is actually pretty useful for me. When I write MVC tests I always have to look for the class where printing handler is placed. With MockMvcPrint enum it is possible to set results printing with System.out (the default one), System.err or logger (on DEBUG level). You can disable printing with NONE option.

The option of using Selenium or HtmlUnit in your tests is very nice. And with auto configuration mechanism you only need to add proper dependencies to your tests’ classpath and you can inject WebDriver and WebClient into your tests to assert HTML pages. This is a valuable addition to the framework.

I have created the BookController class with a couple of tests in my Spring playground to check the annotation. The controller uses the BookService as a business logic implementation that talks to JPA repository. With @MockBean (yet another new thing in Spring Boot v1.4) on the service class, I could easily detach the controller’s tests from database configuration. The ease of enabling Selenium/HtmlUnit tests is impressive as well. With all these improvements WebMvcTest gives us, a developer has way more time to focus on testing behaviour of an application instead of setting a test environment. After this short (yet useful) exercise I have to admit the annotation is a good add-on to the framework.

There is a lot of nice-to-have things in Spring Boot v1.4. If you have this possibility, you should definitely update its version in your project.


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.