我的測試類都被註解如下:運行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'實現了一個接口,它將只能作爲那些接口而不是具體類(它隱藏在代理中)。 –