title: Testing RabbitMQ with Spring and Mockito
body: <p>Attempting to have Spring JUnit runner run test with RabbitTemplate and the listener injected with a Mockito stubbed service class. Trying to verify interaction with the Mock. With the examples I've seen I thought this would be possible. RabbitMQ is working. When logging into the dashboard, I can see the messages there. Am able to consume messages also with standalone console application.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:/spring/servlet-context.xml", "classpath:/spring/root-context.xml", "classpath:rabbitlistener-context-test.xml"})
  public class ReceiveOrderQueueListenerTest {
  
      @Mock
      private ReceiveOrderRepository mockRepos;
  
      @Autowired
      RabbitTemplate rabbitTemplate;
  
      @Autowired
      SimpleMessageListenerContainer  listenerContainer;
  
      @InjectMocks
      @Autowired
      ReceiveOrderQueueListener receiveOrderQueueListener;
  
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void testAbleToReceiveMessage() {
          RequestForService rfs = new RequestForService();
          rfs.setClaimNumber("a claim");
          rabbitTemplate.convertAndSend("some.queue", rfs);
          verify(mockRepos).save(new OrderRequest());
      }
  
  } 
  </code></pre>
  
  <p>then the rabbit-listener config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:rabbit="http://www.springframework.org/schema/rabbit"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
            http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"&gt;
  
       &lt;rabbit:connection-factory id="rabbitConnectionFactory" host="XXXXXXXXX.com"   username="test" password="test" /&gt; 
   &lt;!--    &lt;rabbit:connection-factory id="rabbitConnectionFactory" host="localhost" username="guest" password="guest" /&gt; --&gt;
  
      &lt;rabbit:admin connection-factory="rabbitConnectionFactory" auto-startup="true"  /&gt;
  
      &lt;rabbit:template id="tutorialTemplate" connection-factory="rabbitConnectionFactory"  exchange="TUTORIAL-EXCHANGE"/&gt;
  
      &lt;rabbit:queue name="some.queue"  id="some.queue"&gt;&lt;/rabbit:queue&gt;
  
      &lt;bean id="receiveOrderListener" class="XXXXXXXXXX.connect.message.ReceiveOrderQueueListenerImpl"&gt;&lt;/bean&gt;
  
  
      &lt;rabbit:topic-exchange id="myExchange" name="TUTORIAL-EXCHANGE"&gt;
          &lt;rabbit:bindings&gt;
              &lt;rabbit:binding queue="some.queue"   pattern="some.queue"&gt;&lt;/rabbit:binding&gt;
          &lt;/rabbit:bindings&gt;
      &lt;/rabbit:topic-exchange&gt;
  
      &lt;rabbit:listener-container connection-factory="rabbitConnectionFactory"&gt;
      &lt;!--    &lt;rabbit:listener queues="some.queue" ref="receiveOrderListener" method="handleMessage"/&gt; --&gt;
          &lt;rabbit:listener queues="some.queue" ref="receiveOrderListener" /&gt;
      &lt;/rabbit:listener-container&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><p>
  Tried injecting in the MessgeListenerAdaptor as well thinking the test needed that to wire the listener correct as well. With the adaptor injected in, I am able to verify the delegate gets injected in and with the stub. 
  Test fails on no zero interactions with mock. I can log into rabbitmq and the messages are there. The injected listener object is not consuming messages from the queue during the run of this test.</p>
  
  <p>Almost forgot, the said listener. Tried default signature and tried the designate method. </p>
  
  <pre><code>public class ReceiveOrderQueueListenerImpl implements ReceiveOrderQueueListener {
  
      @Autowired
      ReceiveOrderRepository receiveOrderRepository;
  
      @Override
      public void handleMessage(RequestForService rfs) {
          System.out.println("receive a message");
          receiveOrderRepository.save(new OrderRequest());
      }
  
      public void onMessage(Message message) {
          receiveOrderRepository.save(new OrderRequest());
      }
  
  }
  </code></pre>
  
  <p>Any suggestions would be helpful and I appreciate your help ahead of time.</p>