PDA

View Full Version : Mocking AMQP components for unit tests



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?

Dave Syer
Jun 3rd, 2011, 05:35 AM
Sounds like it's an integration test to me. In Spring AMQP we just suck it up and connect to RabbitMQ, and we have some helper classes that make that less painful (BrokerTestUtils, BrokerRuning in particular), but only in our tests (so not available as a downloadable jar right now). It's the only way to be sure your code is working. The task of mocking the whole Rabbit Java API is not one that I would wish on anyone, but if you feel like contributing something, knock yourself out.