我正在测试一个有价值的类@Qualified @Autowired:
// class under test
class C1() {
@Autowired
@Qualified("c1")
DataSource d1;
...
}
DataSourced1位于配置文件中。
但是当我测试同一个类并且我使用单独的DataSource测试配置类时:
// test for class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = testC1Config.class, loader=AnnotationConfigContextLoader.class)
class testC1() {
@Autowired
@Qualified("c1Test")
DataSource d1Test;
...
}
// testC1Config
@Configuration
class testC1Config() {
@Bean
@Qualified("c1Test")
DataSource c1Test() {
...
}
}
我得到这个例外:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=c1)}
说他只看到只有 Qualifier 的 DataSource Bean c1。尝试使用@Bean(name=c1Test")但我得到相同的结果..
我如何让他看到测试包中的 c1Test DataSource bean?
神不在的星期二
相关分类