5

I'm building a custom starter library which registers an ObjectMapper in a WebMvcConfigurer class. One of the settings on this ObjectMapper is an Instant serialization format.

When I use @SpringBootTest with @AutoConfigureMockMvc the configured ObjectMapper is picked up, and everything works as expected. However the same assertions using @WebMvcTest fail. It seems like the WebMvcConfigurer class is not being picked up in the @WebMvcTest autoconfiguration, although the docs state that it should be picked up.

Is there a way to extend the autoconfiguration for WebMvcTest's without having to resort to putting @Import with every @WebMvcTest annotation?

code example

Torje
  • 55
  • 1
  • 7

1 Answers1

4

Similar to what you have done to include your starter’s configuration in the main auto-configuration, you can include a configuration class in a particular test slice by adding an entry to spring.factories. In this case, you should use the key org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc.

If you look at the Spring Boot source code you can see that this is how Boot’s Jackson auto-configuration is included, for example.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242