title: How can I prevent Spring JmsTemplate unit test from blocking when reading ActiveMQ queue?
body: <p>How can I prevent <a href="http://projects.spring.io/spring-framework/" rel="nofollow noreferrer">Spring</a> <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html" rel="nofollow noreferrer">JmsTemplate</a> unit test <code>sendAndReceivePerson()</code> from blocking when attempting to read a <code>Person</code> object from <a href="http://activemq.apache.org/" rel="nofollow noreferrer">ActiveMQ</a> queue <code>person.queue</code>?</p>
  
  <p>The test creates a <code>Person</code> object, sends it to queue <code>person.queue</code> (this should also create the embedded broker that contains the queue), and then attempts to read the object from the same queue.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {MessagingConfiguration.class})
  public class PersonMessengerTest {
      @Autowired
      private PersonMessenger personMessenger;
  
      @Test
      public void sendAndReceivePerson() {
          final Person person = new Person();
          final UUID id = UUID.randomUUID();
          person.setId(id);
          person.setFirstName("Derek");
          person.setLastName("Mahar");
          personMessenger.sendPersion(person);
          final Person receivedPersion = personMessenger.receivePersion();
          assertEquals(id, receivedPersion.getId());
      }
  }
  
  public class PersonMessenger {
  
      private final JmsOperations jmsOperations;
  
      public PersonMessenger(JmsOperations jmsOperations) {
          this.jmsOperations = jmsOperations;
      }
  
      public void sendPersion(final Person person) {
          this.jmsOperations.convertAndSend(person);
      }
  
      public Person receivePersion() {
          return (Person) this.jmsOperations.receiveAndConvert();
      }
  }
  
  @Configuration
  @Import({CommonConfiguration.class})
  public class MessagingConfiguration {
  
      public static final String PERSON_QUEUE = "person.queue";
  
      @Autowired
      private ApplicationEnvironment applicationEnvironment;
  
      @Bean
      public ConnectionFactory jmsConnectionFactory() {
          final ActiveMQConnectionFactory activeMqConnectionFactory = new ActiveMQConnectionFactory(
                  this.applicationEnvironment.getJmsBrokerUrl());
          activeMqConnectionFactory.setTrustAllPackages(true);
          return activeMqConnectionFactory;
      }
  
      @Bean
      public JmsOperations jmsTemplate(ConnectionFactory connectionFactory) {
          final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
          jmsTemplate.setDefaultDestinationName(PERSON_QUEUE);
          return jmsTemplate;
      }
  
      @Bean
      public PersonMessenger personMessenger(JmsOperations jmsOperations) {
          return new PersonMessenger(jmsOperations);
      }
  
      @Bean(name = PERSON_QUEUE)
      public Queue personQueue() {
          return new ActiveMQQueue(PERSON_QUEUE);
      }
  }
  
  @Configuration
  @PropertySource("classpath:application.properties")
  public class CommonConfiguration {
  
      @Autowired
      private Environment applicationEnvironment;
  
      @Bean
      public ApplicationEnvironment applicationEnvironment() {
          return new ApplicationEnvironment(this.applicationEnvironment);
      }
  }
  </code></pre>
  
  <p>application.properties:</p>
  
  <pre><code>jms.brokerUrl=vm://test?async=false&amp;broker.persistent=false&amp;broker.useJmx=false
  </code></pre>
  
  <p>Notice that the test blocks while attempting to read the <code>Person</code> object from the queue:</p>
  
  <pre><code>-------------------------------------------------------
   T E S T S
  -------------------------------------------------------
  Running com.opessoftware.example.spring.messenger.PersonMessengerTest
  2017-02-15 15:30:33,736|WARN|main|o.a.activemq.broker.BrokerService|49670|
      org.apache.activemq.broker.BrokerService.checkMemorySystemUsageLimits(BrokerService.java:2142)
      Memory Usage for the Broker (1024mb) is more than the maximum available for the JVM: 955 mb - resetting to 70% of maximum available: 668 mb
  2017-02-15 15:30:33,993|WARN|main|o.a.activemq.broker.BrokerService|49927|
      org.apache.activemq.broker.BrokerService.checkMemorySystemUsageLimits(BrokerService.java:2142)
      Memory Usage for the Broker (1024mb) is more than the maximum available for the JVM: 955 mb - resetting to 70% of maximum available: 668 mb
  </code></pre>
  
  <p>Might the <code>receiveAndConvert()</code> call be creating a second embedded broker or might it be reading from a different queue?</p>