单元测试报错 NoSuchBeanDefinitionException: No qualifying bean of type
问题写单元测试时,使用@Autowire注解自动注入一个类@Autowiredprivate PrometheusClient prometheusClient;发现显示错误:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.esho
·
问题
写单元测试时,使用@Autowire注解自动注入一个类
@Autowired
private PrometheusClient prometheusClient;
发现显示错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.eshore.paas.ops.serv.services.common.PrometheusClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
错误信息显示是因为找不到这个bean导致的。
解决
最后发现是@SpringBootTest注解中使用的类配置错了,
实际应该写SecondAlertApplication.class这个类,才会自动扫描该类目录下的所有包。
修改后配置:
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
//此处修改
@SpringBootTest(classes = SecondAlertApplication.class)
@Transactional
@Rollback
@Slf4j
public class secondAlertTest {
//....代码省略
}
注:
完整的一个单元测试样例:
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)