web-dev-qa-db-fra.com

Quelle est la différence entre @AutoConfigureWebMvc et @AutoConfigureMockMvc?

Dans quel cas dois-je utiliser chacun d'eux?

10
Ekaterina

@ AutoConfigureWebMvc

Utilisez-le si vous devez configurer la couche Web pour les tests mais n'avez pas besoin d'utiliser MockMvc

Il active toutes les configurations automatiques liées à la couche Web et [~ # ~] seulement [~ # ~] à la couche Web. Il s'agit d'un sous-ensemble de la configuration automatique globale.

Il comprend la configuration automatique suivante (voir 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

Utilisez-le lorsque vous souhaitez simplement configurer MockMvc

Active toutes les configurations automatiques liées à MockMvc et [~ # ~] uniquement [~ # ~] MockMvc. Encore une fois, il s'agit d'un sous-ensemble de la configuration automatique globale.

Il comprend la configuration automatique suivante (voir 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

Comprend à la fois le @AutoConfigureWebMvc et le @AutoConfigureMockMvc, entre autres fonctionnalités.

13
dustin.schultz