Source:
<code>public void testLazyContainerWithDest() throws Exception { BeanFactory beanFactory = mock(BeanFactory.class); when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.initialize(); when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)) .thenReturn(scheduler); final JmsOutboundGateway gateway = new JmsOutboundGateway(); gateway.setBeanFactory(beanFactory); gateway.setConnectionFactory(getGatewayConnectionFactory()); gateway.setRequestDestination(requestQueue7); gateway.setReplyDestination(replyQueue7); gateway.setCorrelationKey("JMSCorrelationID"); gateway.setUseReplyContainer(true); gateway.setIdleReplyContainerTimeout(1, TimeUnit.SECONDS); gateway.afterPropertiesSet(); gateway.start(); Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { JmsTemplate template = new JmsTemplate(); template.setConnectionFactory(getTemplateConnectionFactory()); template.setReceiveTimeout(10000); receiveAndSend(template); receiveAndSend(template); } private void receiveAndSend(JmsTemplate template) { javax.jms.Message request = template.receive(requestQueue7); final javax.jms.Message jmsReply = request; try { template.send(request.getJMSReplyTo(), new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return jmsReply; } }); } catch (JmsException e) { } catch (JMSException e) { } } }); assertNotNull(gateway.handleRequestMessage(new GenericMessage<String>("foo"))); DefaultMessageListenerContainer container = TestUtils.getPropertyValue(gateway, "replyContainer", DefaultMessageListenerContainer.class); <START> Thread.sleep(3000); <END> assertFalse(container.isRunning()); assertNotNull(gateway.handleRequestMessage(new GenericMessage<String>("foo"))); assertTrue(container.isRunning()); gateway.stop(); assertFalse(container.isRunning()); }</code><technical_language>do a long timeout in tests... CountDownLatch for some mocked method?.</technical_language>

Target:
public void testLazyContainerWithDest() throws Exception { BeanFactory beanFactory = mock(BeanFactory.class); when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.initialize(); when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)) .thenReturn(scheduler); final JmsOutboundGateway gateway = new JmsOutboundGateway(); gateway.setBeanFactory(beanFactory); gateway.setConnectionFactory(getGatewayConnectionFactory()); gateway.setRequestDestination(requestQueue7); gateway.setReplyDestination(replyQueue7); gateway.setCorrelationKey("JMSCorrelationID"); gateway.setUseReplyContainer(true); gateway.setIdleReplyContainerTimeout(1, TimeUnit.SECONDS); gateway.afterPropertiesSet(); gateway.start(); Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { JmsTemplate template = new JmsTemplate(); template.setConnectionFactory(getTemplateConnectionFactory()); template.setReceiveTimeout(10000); receiveAndSend(template); receiveAndSend(template); } private void receiveAndSend(JmsTemplate template) { javax.jms.Message request = template.receive(requestQueue7); final javax.jms.Message jmsReply = request; try { template.send(request.getJMSReplyTo(), new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return jmsReply; } }); } catch (JmsException e) { } catch (JMSException e) { } } }); assertNotNull(gateway.handleRequestMessage(new GenericMessage<String>("foo"))); DefaultMessageListenerContainer container = TestUtils.getPropertyValue(gateway, "replyContainer", DefaultMessageListenerContainer.class); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); assertNotNull(gateway.handleRequestMessage(new GenericMessage<String>("foo"))); assertTrue(container.isRunning()); gateway.stop(); assertFalse(container.isRunning()); }