Annotation Type WebMvcTest


  • @Target(TYPE)
    @Retention(RUNTIME)
    @Documented
    @Inherited
    @BootstrapWith(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper.class)
    @ExtendWith(org.springframework.test.context.junit.jupiter.SpringExtension.class)
    @OverrideAutoConfiguration(enabled=false)
    @TypeExcludeFilters(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTypeExcludeFilter.class)
    @AutoConfigureCache
    @AutoConfigureWebMvc
    @AutoConfigureMockMvc
    @ImportAutoConfiguration
    public @interface WebMvcTest
    Annotation that can be used in combination with @RunWith(SpringRunner.class) for a typical Spring MVC test. Can be used when a test focuses only on Spring MVC components.

    Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e. @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component, @Service or @Repository beans).

    By default, tests annotated with @WebMvcTest will also auto-configure Spring Security and MockMvc (include support for HtmlUnit WebClient and Selenium WebDriver). For more fine-grained control of MockMVC the @AutoConfigureMockMvc annotation can be used.

    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.

    Since:
    1.4.0
    See Also:
    AutoConfigureWebMvc, AutoConfigureMockMvc, AutoConfigureCache
    • Optional Element Summary

      Optional Elements 
      Modifier and TypeOptional ElementDescription
      Class<?>[]controllers
      Specifies the controllers to test.
      Class<?>[]excludeAutoConfiguration
      Auto-configuration exclusions that should be applied for this test.
      org.springframework.context.annotation.ComponentScan.Filter[]excludeFilters
      A set of exclude filters which can be used to filter beans that would otherwise be added to the application context.
      org.springframework.context.annotation.ComponentScan.Filter[]includeFilters
      A set of include filters which can be used to add otherwise filtered beans to the application context.
      String[]properties
      Properties in form key=value that should be added to the Spring Environment before the test runs.
      booleansecure
      Deprecated.
      since 2.1.0 in favor of Spring Security's testing support
      booleanuseDefaultFilters
      Determines if default filtering should be used with @SpringBootApplication.
      Class<?>[]value
      Specifies the controllers to test.
    • Element Detail

      • properties

        String[] properties
        Properties in form key=value that should be added to the Spring Environment before the test runs.
        Returns:
        the properties to add
        Since:
        2.1.0
        Default:
        {}
      • value

        @AliasFor("controllers")
        Class<?>[] value
        Specifies the controllers to test. This is an alias of controllers() which can be used for brevity if no other attributes are defined. See controllers() for details.
        Returns:
        the controllers to test
        See Also:
        controllers()
        Default:
        {}
      • controllers

        @AliasFor("value")
        Class<?>[] controllers
        Specifies the controllers to test. May be left blank if all @Controller beans should be added to the application context.
        Returns:
        the controllers to test
        See Also:
        value()
        Default:
        {}
      • useDefaultFilters

        boolean useDefaultFilters
        Determines if default filtering should be used with @SpringBootApplication. By default only @Controller (when no explicit controllers are defined), @ControllerAdvice and WebMvcConfigurer beans are included.
        Returns:
        if default filters should be used
        See Also:
        includeFilters(), excludeFilters()
        Default:
        true
      • includeFilters

        org.springframework.context.annotation.ComponentScan.Filter[] includeFilters
        A set of include filters which can be used to add otherwise filtered beans to the application context.
        Returns:
        include filters to apply
        Default:
        {}
      • excludeFilters

        org.springframework.context.annotation.ComponentScan.Filter[] excludeFilters
        A set of exclude filters which can be used to filter beans that would otherwise be added to the application context.
        Returns:
        exclude filters to apply
        Default:
        {}
      • secure

        @Deprecated
        @AliasFor(annotation=AutoConfigureMockMvc.class)
        boolean secure
        Deprecated.
        since 2.1.0 in favor of Spring Security's testing support
        If Spring Security's MockMvc support should be auto-configured when it is on the classpath. Also determines if WebSecurityConfigurer classes should be included in the application context. Defaults to true.
        Returns:
        if Spring Security's MockMvc support is auto-configured
        Default:
        true
      • excludeAutoConfiguration

        @AliasFor(annotation=org.springframework.boot.autoconfigure.ImportAutoConfiguration.class,
                  attribute="exclude")
        Class<?>[] excludeAutoConfiguration
        Auto-configuration exclusions that should be applied for this test.
        Returns:
        auto-configuration exclusions to apply
        Default:
        {}