38

In which case should I use each one?

Ekaterina
  • 1,642
  • 3
  • 19
  • 36
  • Possible duplicate of [How can I use @WebMvcTest and also add in my own custom filters?](https://stackoverflow.com/questions/38746850/how-can-i-use-webmvctest-and-also-add-in-my-own-custom-filters) – Sam Sep 21 '17 at 12:30

1 Answers1

44

@AutoConfigureWebMvc

Use this if you need to configure the web layer for testing but don't need to use MockMvc

It enables all auto-configuration related to the web layer and ONLY the web layer. This is a subset of overall auto-configuration.

It includes the following auto-configuration (see spring.factories)

# AutoConfigureWebMvc auto-configuration imports
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc=\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

@AutoConfigureMockMvc

Use this when you just want to configure MockMvc

Enables all auto-configuration related to MockMvc and ONLY MockMvc. Again, this is a subset of overall auto-configuration.

It includes the following auto-configuration (see spring.factories)

# AutoConfigureMockMvc auto-configuration imports
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration,\
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration

@WebMvcTest

Includes both the @AutoConfigureWebMvc and the @AutoConfigureMockMvc, among other functionality.

dustin.schultz
  • 13,076
  • 7
  • 54
  • 63