我已经通过运行单元测试验证了CandidateRepository的工作正常。但是,当我尝试运行CandidateController的集成测试时,我一直收到一个错误,说“No qualifying bean of type 'CandidateRepository' available”。我不确定为什么会发生这种情况,因为我已经包含了必要的注解(@Repository,@Service,and @RestController)and component scanning in my code. Any idea on how to fix this issue?
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条答案
按热度按时间bnlyeluc1#
确保将@Service和@Transactional放在CandidateServiceImpl类之上。