Results 1 to 2 of 2

Thread: Mocking AMQP components for unit tests

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Posts
    1

    Default 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

    Code:
    @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?

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •