Announcement Announcement Module
No announcement yet.
Mocking AMQP components for unit tests Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mocking AMQP components for unit tests

    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

        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;
                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?

  • #2
    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.