In which case should I use each one?
Annotation Type AutoConfigureMockMvc Annotation that can be applied to a test class to enable and configure auto-configuration of MockMvc .
Typically @WebMvcTest is used in combination with @MockBean or @Import to create any collaborators required by your @Controller beans. If you are looking to load your full application configuration and use MockMVC, you should consider @SpringBootTest combined with @AutoConfigureMockMvc rather than this annotation.
@AutoConfigureMockMvc Enables all auto-configuration related to MockMvc and ONLY MockMvc . Again, this is a subset of overall auto-configuration.
The MockMvcBuilders. standaloneSetup allows to register one or more controllers without the need to use the full WebApplicationContext .
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With