我的测试类都被注解如下:运行AnnotationConfigApplicationContext从弹簧试验
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional(propagation= Propagation.REQUIRED)
@ContextConfiguration(classes = { TestLocalPersisterConfiguration.class })
@ActiveProfiles(EnvironmentProfile.TEST_LOCAL)
public class MyTestClass {
// run someMethod here that loads AnnotationConfigApplicationContext in Java class
}
从测试I类运行从主类中的方法,并尝试加载AnnotationConfigApplicationContext`:
// Java class method that is run from test class
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestLocalPersisterConfiguration.class, ProdPersisterConfiguration.class);
GCThreadStopRepository repository = applicationContext.getBean(GCThreadStopRepository.class);
但是,春天抱怨No qualifying bean of type [ca.nbc.data.sql.repository.GCThreadStopRepository] is defined
我不知道为什么会发生这种情况,以及如何解决这个问题。
GCThreadStopRepository
注有@Repository
。
TestLocalPersisterConfiguration
延伸GenericPersisterConfiguration
,它具有以下扫描和负载bean定义:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
String persistenceUnitName = environment.getProperty(PROPERTY_PERSISTENCE_UNIT);
final LocalContainerEntityManagerFactoryBean emfBean = new LocalContainerEntityManagerFactoryBean();
emfBean.setPersistenceUnitName(persistenceUnitName);
emfBean.setPackagesToScan("ca.nbc.data.sql");
emfBean.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
emfBean.setDataSource(dataSource());
if(getJpaProperties() != null) {
emfBean.setJpaProperties(getJpaProperties());
}
return emfBean;
}
UPDATE:
我发现,当AnnotationConfigApplicationContext
在Java类被启动时,@ActiveProfiles(EnvironmentProfile.TEST_LOCAL)
从测试类设置不传播到Java类,即。在Java类中运行applicationContext.getEnvironment().getActiveProfiles()
会返回一个空数组。
有没有办法将@ActiveProfiles(EnvironmentProfile.TEST_LOCAL)
传播到系统范围?
究竟为什么你自己加载它?你似乎缺少'@ ContextConfiguration'和基于spring的测试类的观点。在那个Spring旁边使用了基于代理的应用程序,所以如果你的'GCThreadStopRepository'实现了一个接口,它将只能作为那些接口而不是具体类(它隐藏在代理中)。 –