If you are using @WebMvcTest for your test , it means you are focusing mainly on testing the spring mvc layer and not going any deeper into the application.
So this annotation can be used only when a test focuses on Spring
MVC components. 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.
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ CustomerConfig.class, SchedulerConfig.class })
public class AppConfig {
}
You can then import this configuration class using @import
in the @WebMvcTest
annotated test class and the beans should be picked up by spring.
Reference : https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.html