Annotation Type WebMvcTest


@Documented @Inherited @Retention(RUNTIME) @Target(TYPE) @ResourceLock("com.codename1.backend.test.BackendTest") public @interface WebMvcTest

Tests controllers without the rest of the application: what Spring's @WebMvcTest does.

The build wires the controllers this names -- every controller when it names none -- with what serves their requests: the @ControllerAdvice and @RestControllerAdvice classes, and the HandlerInterceptor and ExceptionResolver components. No service, repository or database is built. Whatever a controller needs beyond that comes from the test, as a MockitoBean, a TestConfiguration bean, or a class named in includes(). A dependency nothing supplies fails the build, naming the controller and the type it asked for, so a slice never starts half wired.

The test reaches the controllers through MockMvc, in process.

@WebMvcTest(GreetingApi.class)
class GreetingApiSliceTest {
    @Autowired MockMvc mvc;
    @MockitoBean Greetings greetings;
    ...
}
  • Element Details

    • value

      Class<?>[] value
      The controllers to test; every controller of the module when empty.
      Default:
      {}
    • controllers

      Class<?>[] controllers
      The same as value(), for a test that reads better naming it.
      Default:
      {}
    • includes

      Class<?>[] includes
      Components and configurations of the application to wire into the slice as well -- what Spring's @Import adds to one. Each must be a bean class of the module.
      Default:
      {}
    • properties

      String[] properties
      Settings for this test, as key=value, as BackendTest.properties().
      Default:
      {}
    • profile

      String profile
      The profile, as BackendTest.profile().
      Default:
      "test"
    • classes

      Class<?>[] classes
      Default:
      {}