But you will discover a part of them only randomly when you will encounter problems such as bean initialization exception during the spring boot context init or a NullPointerException
rising during the test execution.
To make things simpler, focus on intention.
When you write that :
@SpringBootTest(classes = SomeController.class)
you will make Spring to init only the SomeController bean instance.
Is it desirable to test a controller ?
Probably no since you need a way to invoke the controller with a controller approach.
For that a MockMvc instance would help.With WebMvcTest you get that bean additionally loaded in the test context.
So that way is preferable :
@WebMvcTest(SomeController.class)public class SomeControllerTest{@Autowiredprivate MockMvc mvc;...}
Of course you could get a similar behavior with @SpringBootTest
and some additional classes but it will be just an overhead : the @WebMvcTest
specialized annotation is enough.
At last why make the reading of the test class harder for your follower ?
By weaving a contrived way of using spring boot test annotation, chances are good to come there.
I think for answering your question enough just read the Javadoc for both of these annotations:
1. @WebMvcTest
Annotation that can be used for a Spring MVC test that 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.
@SpringbootTest
Annotation that can be specified on a test class that runs Spring Boot based tests. Provides the following features over and above the regular Spring TestContext Framework:
Uses SpringBootContextLoader as the default ContextLoader when no specific @ContextConfiguration(loader=...)
is defined.Automatically searches for a @SpringBootConfiguration when nested @Configuration
is not used, and no explicit classes are specified.Allows custom Environment properties to be defined using the properties attribute.
Allows application arguments to be defined using the args attribute.Provides support for different webEnvironment modes, including the ability to start a fully running web server listening on a defined or random port.Registers a TestRestTemplate and/or WebTestClient bean for use in web tests that are using a fully running web server.