Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between @AutoConfigureWebMvc and @AutoConfigureMockMvc?

Tags:

In which case should I use each one?

like image 637
Ekaterina Avatar asked Sep 21 '17 12:09

Ekaterina


People also ask

What does AutoConfigureMockMvc annotation do?

Annotation Type AutoConfigureMockMvc Annotation that can be applied to a test class to enable and configure auto-configuration of MockMvc .

What is the use of @WebMvcTest?

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.

What is @autoconfigurewebmvc?

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

What is MockMvcBuilders standaloneSetup?

The MockMvcBuilders. standaloneSetup allows to register one or more controllers without the need to use the full WebApplicationContext .


1 Answers

@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.

like image 149
dustin.schultz Avatar answered Sep 18 '22 13:09

dustin.schultz