其他分享
首页 > 其他分享> > spring – @AutoConfigureWebMvc和@AutoConfigureMockMvc之间有什么区别?

spring – @AutoConfigureWebMvc和@AutoConfigureMockMvc之间有什么区别?

作者:互联网

在哪种情况下我应该使用每一个?

解决方法:

@AutoConfigureWebMvc

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

它启用与Web层相关的所有自动配置,并且仅启用Web层.这是整体自动配置的子集.

它包括以下自动配置(参见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

启用与MockMvc和ONLY MockMvc相关的所有自动配置.同样,这是整体自动配置的子集.

它包括以下自动配置(参见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

包括@AutoConfigureWebMvc和@AutoConfigureMockMvc以及其他功能.

标签:spring,spring-boot-2,spring-boot-test
来源: https://codeday.me/bug/20190722/1500940.html