You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3 JUnit 5集成测试:CandidateRepository Bean创建失败问题

修复CandidateController集成测试中CandidateRepository Bean找不到的问题

问题重现

执行CandidateController集成测试时抛出如下错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'candidateController' defined in file [D:\java_repos\micro-library\machine_learning_java\talent-sourcing-system\build\classes\java\main\com\tss\talentsourcingsystem\application\candidate\controller\CandidateController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'candidateServiceImpl' defined in file [D:\java_repos\micro-library\machine_learning_java\talent-sourcing-system\build\classes\java\main\com\tss\talentsourcingsystem\application\candidate\service\impl\CandidateServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'com.tss.talentsourcingsystem.application.candidate.repository.CandidateRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

当前集成测试代码:

@ExtendWith(MockitoExtension.class)
@SpringBootTest
@Transactional
class CandidateControllerIntegrationTest {

    private static final String BASE_URL = "http://localhost:8081/api/v1/candidates";

    private MockMvc mockMvc;
    private ObjectMapper objectMapper;

    @Autowired
    private WebApplicationContext context;

    @Autowired
    private CandidateRepository candidateRepository;

    @Autowired
    private CandidateService candidateService;

    @Autowired
    private CandidateController candidateController;

    @BeforeEach
    void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
        this.objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
    }

    // tests here

}

可行修复方案

1. 移除冲突的测试扩展注解

@ExtendWith(MockitoExtension.class) 和 @SpringBootTest 同时使用会导致Spring测试上下文加载异常——MockitoExtension会接管测试上下文,而非加载完整的Spring应用上下文。直接移除该注解即可:

// 移除这一行:@ExtendWith(MockitoExtension.class)
@SpringBootTest
@Transactional
class CandidateControllerIntegrationTest {
    // ... 其余代码不变
}

2. 确保@SpringBootTest扫描到主应用类

如果你的Spring Boot主启动类(比如TalentSourcingSystemApplication.java)不在当前测试类的上级包路径中,需要显式指定主类,让Spring正确扫描所有组件:

@SpringBootTest(classes = TalentSourcingSystemApplication.class)
@Transactional
class CandidateControllerIntegrationTest {
    // ... 其余代码不变
}

3. 验证CandidateRepository的定义

确保CandidateRepository接口继承了Spring Data提供的Repository接口(如JpaRepository、CrudRepository),否则Spring无法自动生成代理Bean:

@Repository
public interface CandidateRepository extends JpaRepository<Candidate, Long> {
    // ... 自定义方法
}

4. 正确使用@MockBean(如需模拟Repository)

如果你的测试不需要真实数据库交互,而是要模拟Repository行为,不要用@Autowired注入真实实例,改用@MockBean注入Mock对象:

@SpringBootTest
@Transactional
class CandidateControllerIntegrationTest {

    // 替换@Autowired为@MockBean
    @MockBean
    private CandidateRepository candidateRepository;

    @Autowired
    private CandidateService candidateService;

    // ... 其余代码不变

    @BeforeEach
    void setUp() {
        // 可以在这里配置Mock的行为,比如:
        when(candidateRepository.save(any(Candidate.class))).thenReturn(new Candidate());
        // ...
    }
}

内容的提问来源于stack exchange,提问作者Meftun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 01:27:02