在那种情况下我应该使用每个?
最佳答案
@AutoConfigureWebMvc
如果您需要配置Web层以进行测试,而无需进行配置,请使用此选项
需要使用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
当您只想配置
MockMvc
时使用此选项启用与
MockMvc
和仅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 - @AutoConfigureWebMvc和@AutoConfigureMockMvc有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46343782/