title: JMS Testing - JMSTemplate send not executed
body: <p>I have code piece sending jms messages via Spring JMSTemplate. For testing the the method i use Mockito.</p>
  
  <p>My code looks like following.... publishDialogueServiceMessage()-></p>
  
  <pre><code> brokerUrl = jmsQueueProperties.getProperty(MessageRouterConstants.JMS_QUEUE_URL);  
          LOG.info("The broker url is : {}", brokerUrl);  
          jmsTemplate.send(jmsQueueProperties.getProperty(MessageRouterConstants.QUEUE), new MessageCreator() {
  
              @Override
              public Message createMessage(Session session) throws JMSException {             
                  ObjectMessage obj = session.createObjectMessage(serviceResponse);
                  messageSent = true;
                  return obj;
              }
          });
  </code></pre>
  
  <p>In above code to i set boolean variable true, to check that if the message is sent</p>
  
  <p>My Test looks following,</p>
  
  <pre><code>@Before
      public void setUp() throws Exception {
  
          connectionFactory = Mockito.spy(new ActiveMQConnectionFactory(
                  "vm://localhost?broker.persistent=false"));
          conn = connectionFactory.createConnection();
          conn.start();       
      } 
  
  @After
  public void cleanUp() throws Exception{
      conn.stop();
  }
  </code></pre>
  
  <p><br/></p>
  
  <pre><code>@Test
      public void testPublishDialogueServiceMessage()
      {
          ServiceResponse response = Mockito.mock(
                  ServiceResponse.class, Mockito.withSettings()
                          .serializable());
          JmsTemplate mockTemplate = Mockito.mock(JmsTemplate.class);
          java.util.Properties p = Mockito.mock(java.util.Properties.class);      
          Mockito.when(p.getProperty(MessageRouterConstants.QUEUE))
                  .thenReturn("outbound.request.queue");
          mockTemplate.setConnectionFactory(connectionFactory);
          mockTemplate.setDeliveryPersistent(true);
          mockTemplate.setSessionAcknowledgeMode(2);
          mockTemplate.setSessionTransacted(true);
  
          ReflectionTestUtils.setField(publisher, "jmsQueueProperties", p);
          ReflectionTestUtils.setField(publisher, "jmsTemplate", mockTemplate);
  
          // test
          publisher.publishDialogueServiceMessage(response);
          ArgumentCaptor&lt;MessageCreator&gt; msgCreator = ArgumentCaptor.forClass(MessageCreator.class);
          Mockito.verify(p, Mockito.times(2))
                  .getProperty(Mockito.anyString());
          Mockito.verify(mockTemplate, Mockito.times(1)).send(
                  Mockito.anyString(), Mockito.any(MessageCreator.class));
  
          //MessageCreator msgCrt = Mockito.spy(msgCreator.getValue());
          //Assert.notNull(msgCrt);
  
          Assert.isTrue(publisher.isMessageSent());
      }
  </code></pre>
  
  <p>In test i facing an interesting problem as <em>publisher.isMessageSent()</em> always returns me FALSE indicating that send message seems not executed(?). but <em>Mockito.verify(mockTemplate, Mockito.times(1)).send(Mockito.anyString(), Mockito.any(MessageCreator.class));</em> goes fine.</p>
  
  <p>I am wondering what is the cause that my messageSent variable not setting. Can anyone shed some light what I might be doing wrong. </p>