JUnit5测试场景下Service依赖无法注入 Spring Boot主程序运行时注入正常
问题根因
- 主程序运行时,Spring会全量扫描项目路径下的所有组件,@Service注解的业务类会被正常装配为Bean,因此接口访问和依赖注入都没有问题。
- 你使用的
@WebMvcTest是Spring Boot提供的Web层切片测试注解,设计初衷是只加载与Web请求处理相关的组件(包括@Controller、@ControllerAdvice、WebMvc配置类、序列化转换器等),完全不会扫描非Web层的@Service、@Repository、@Entity类,因此测试上下文里根本不存在你编写的Service Bean,控制器初始化时自然会抛出找不到Bean的异常。 - 测试类上手动添加的
@ExtendWith(MockitoExtension.class)属于冗余配置:@WebMvcTest已经集成了JUnit5环境下的Spring测试扩展支持,且内置了Mockito的依赖注入能力,叠加多余的扩展注解可能和Spring测试生命周期产生冲突,进一步加剧Bean加载异常。
JUnit5场景下的修复方案
根据你的测试目标选择对应方案,不需要依赖JUnit4的老写法(比如@RunWith(SpringRunner.class)),完全适配你当前排除junit-vintage引擎的纯JUnit5环境:
方案1:仅做Web层接口逻辑测试(匹配@WebMvcTest的设计初衷)
这种场景下不需要加载真实的业务实现,只需要将Controller依赖的Service以Mock形式注入测试上下文即可,配置步骤:
- 删除测试类上冗余的
@ExtendWith(MockitoExtension.class)注解 - 在测试类中为Controller依赖的Service添加
@MockBean注解,Spring会自动在测试上下文生成Mock实例并注入到Controller中 - 可在
@WebMvcTest注解后指定需要加载的Controller类,避免加载无关的Web组件加快测试速度
示例代码:
import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.MockMvc; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; // 指定只加载需要测试的Controller类,缩小扫描范围 @WebMvcTest(ExampleController.class) @AutoConfigureMockMvc(addFilters = false) public class SampleTest { @Autowired private MockMvc mockMvc; // 声明Controller依赖的Service为MockBean,自动完成注入 @MockBean private ExampleService exampleService; @Test void sampleTesting() throws Exception { // 定义Mock方法的返回值 when(exampleService.getData()).thenReturn("test data"); // 发起MockMvc请求验证接口逻辑 mockMvc.perform(get("/example")) .andExpect(status().isOk()); } }
方案2:测试完整业务链路(需要走真实的Service、Repository逻辑)
这种场景下@WebMvcTest的窄切片加载模式不适合,需要切换到全量上下文加载模式,配置步骤:
- 将类上的
@WebMvcTest替换为@SpringBootTest,这个注解会启动和主程序逻辑一致的全量Spring应用上下文,所有项目内的Bean都会被正常扫描装配 - 保留
@AutoConfigureMockMvc(addFilters = false)注解,即可正常使用MockMvc发起请求测试完整链路
示例代码:
import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc(addFilters = false) public class SampleTest { @Autowired private MockMvc mockMvc; @Test void sampleTesting() throws Exception { // 直接发起请求,此时会走真实的Service、Repository、数据库逻辑 mockMvc.perform(get("/example")) .andExpect(status().isOk()); } }
常见踩坑提醒
- 纯JUnit5环境下不需要额外添加任何Runner或扩展注解来适配Spring测试,只要使用Spring Boot 2.2+版本,官方的测试注解已经完全兼容JUnit5
- 不要在
@WebMvcTest类上同时叠加@ExtendWith(MockitoExtension.class),如需使用Mockito的静态Mock能力,直接通过Spring Boot Test提供的@MockBean、@SpyBean注解即可,不需要手动引入独立的Mockito扩展 - 你当前使用的Spring Boot 2.3.0.RELEASE版本对JUnit5的适配已经非常完善,只要不要混用JUnit4的注解写法,不会出现扩展冲突的问题
内容的提问来源于stack exchange,提问作者Agent47
相关产品推荐
相关产品推荐

