WebMvcTest in Spring Boot

后端 未结 1 1756
孤街浪徒
孤街浪徒 2021-01-05 01:38

I have a basic SpringBoot app., embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.

I have this class:

@Service
public         


        
1条回答
  •  有刺的猬
    2021-01-05 01:59

    The whole point of WebMvcTest is to only load the MVC components. Other components must be mocked. Read the documentation:

    To test Spring MVC controllers are working as expected you can use the @WebMvcTest annotation. @WebMvcTest will auto-configure the Spring MVC infrastructure and limit scanned beans to @Controller, @ControllerAdvice, @JsonComponent, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver. Regular @Component beans will not be scanned when using this annotation.

    Often @WebMvcTest will be limited to a single controller and used in combination with @MockBean to provide mock implementations for required collaborators.

    0 讨论(0)
提交回复
热议问题