juise
Jun 3rd, 2011, 02:06 AM
I'm looking for best practices for how to mock AMQP components in unit tests. In particular when starting spring container in unit tests and I wouldn't like to have real connection to RabbitMQ. At the moment I have custom bean post processor that just makes mock objects of the component like
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof AmqpAdmin){
return mock(AmqpAdmin.class);
}else if(bean instanceof SingleConnectionFactory){
return mock(SingleConnectionFactory.class);
}else if(bean instanceof AmqpTemplate){
return mock(AmqpTemplate.class);
}else if(bean instanceof SimpleMessageListenerContainer){
return null;
}else{
return bean;
}
}
I don't like this too much since MessageListenerContainers needs to be removed from the context because some of the calls to mock MessageListenerContainer causes nullpointers. Optimal solution for this would be that Spring AMQP project would offer mock connection factory, amqp admin and amqp template.
Any other ideas for this?
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof AmqpAdmin){
return mock(AmqpAdmin.class);
}else if(bean instanceof SingleConnectionFactory){
return mock(SingleConnectionFactory.class);
}else if(bean instanceof AmqpTemplate){
return mock(AmqpTemplate.class);
}else if(bean instanceof SimpleMessageListenerContainer){
return null;
}else{
return bean;
}
}
I don't like this too much since MessageListenerContainers needs to be removed from the context because some of the calls to mock MessageListenerContainer causes nullpointers. Optimal solution for this would be that Spring AMQP project would offer mock connection factory, amqp admin and amqp template.
Any other ideas for this?