Mixing XML and java beans
Hi,
I wanted to use existing xml beans with the newer (well, to our organization) @Configuration and @Bean annotations, eg
Code:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev")
@ContextConfiguration(locations="classpath:/com/bank/config/xml/account-repository-config.xml")
@ContextConfiguration(classes={
TransferServiceConfig.class,//pretend like accountRepository is not here
StandaloneDataConfig.class,
JndiDataConfig.class})
public class IntegrationTests {
@Autowired
private TransferService transferService;
@Autowired
private AccountRepository accountRepository;
@Test
public void transferTenDollars() throws InsufficientFundsException {
// AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// ctx.getEnvironment().setDefaultProfiles("dev");
// ctx.register(TransferServiceConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
// ctx.refresh();
//
// TransferService transferService = ctx.getBean(TransferService.class);
... continue on with example
Taken from the examples that introduce profiles and modified for a use case I'm considering. Now there are beans that I would not want to configure in java because that means a recompile to change (yes, I know, "profiles", just go with me here). There are also beans where it might be a good idea to construct them in java. Consider this, where stuff happens and sometimes there's a bean wiring problem:
Code:
@Configuration
@Profile("TEST")
public class MyDAOConfig {
@Bean
public MyDAO getMyDAO() {
try {
return new DefaultMyDAO(getMissingService(), getWorkingService());
}
catch (java.lang.NoClassDefFoundError e) {
logger.warn("Could not load some class for the MyDAO, probably DefaultMyDAO, but this is TEST, so mock it out and continue", e);
return new MockMyDAO(getMockService());
}
catch (org.springframework.beans.factory.NoSuchBeanDefinitionException e) {
logger.warn("Could not find some bean for the MyDAO, but this is TEST, so mock it out and continue", e);
return new MockMyDAO(getMockService());
}
}
}
The basic idea is use the regular bean if possible, or mock it out if not possible. That way, we can continue the integration test and still be aware of this particular problem which may have nothing to do with what we are testing.
But the problem is that of course we can't have 2 @ContextConfiguration nor can I have both locations and classes in one annotation, else it throws 2013-01-16 17:45:19,844 ERROR [org.springframework.test.context.ContextConfigurat ionAttributes] - <Test class [com.bank.config.code.IntegrationTests] has been configured with @ContextConfiguration's 'locations' (or 'value') {classpath:/com/bank/config/xml/transfer-service-config.xml} and 'classes' {class com.bank.config.code.TransferServiceConfig, class com.bank.config.code.StandaloneDataConfig, class com.bank.config.code.JndiDataConfig} attributes. Only one declaration of resources is permitted per @ContextConfiguration annotation.>
Any ideas on how to do what I want, or should this be put on the feature wishlist?