I have separate configuration classes that creates different beans per Spring Profile
. I want my tests to simulate production code as much as possible so I want to use the same beans created under the Spring ‘PROD’ Profile
but add/update different attributes specifically used for testing. What is the best way to achieve that?
Example of what I mean:
@Profile(PROD)
public class ProdConfig {
@Bean
public SimpleRabbitListenerContainerFactory containerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMaxConcurrentConsumers(2);
...
return factory;
}
}
@Profile(TEST)
public class TestConfig {
@Bean
public SimpleRabbitListenerContainerFactory containerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMaxConcurrentConsumers(2);
...
/**
The advice chain should only set for testing purposes
**/
factory.setAdviceChain(interceptor());
return factory;
}
@Bean
RetryOperationsInterceptor interceptor() {
...
}
}
Ideally I don't want to duplicate the code to set various properties of SimpleRabbitListenerContainerFactory
in TestConfig
. I just want to load the ProdConfig but call setAdviceChain(interceptor())
.
Try something like this:
@Import(ProdConfig.class)
public class TestConfig {
@Autowired
private SimpleRabbitListenerContainerFactory containerFactory;
@Bean
RetryOperationsInterceptor interceptor() {
...
this.containerFactory.setAdviceChain(interceptor);
return interceptor;
}
}