Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MockRunner with JMS Spring Unit Test

DZone's Guide to

MockRunner with JMS Spring Unit Test

This article shows how to mock your JMS infrastructure using MockRunner and test it using Spring.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

This article shows how to mock your JMS infrastructure using MockRunner and test it using Spring. It is easy to test without JMS infrastructure such as Active MQ, OpenJMS, etc. Mockrunner framework simplify JMS while your in stage to implement JMS or trying to re-create production issue without dependency.

 In this article I show how to create simple Text Message Listener and send a message to queue. 


  1. Requirements:




pom.xml  dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples</groupId>
  <artifactId>spring-jms-timer</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>

</properties>

<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>

<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>

<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${springframework.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
<!--  mockrunner -->
<dependency>
<groupId>com.mockrunner</groupId>
<artifactId>mockrunner-jms</artifactId>
<version>1.0.3</version>
</dependency>

</dependencies>
</project>

Spring Bean Configuration  file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jmshttp://www.springframework.org/schema/jms/spring-jms-3.0.xsd">

<!--  mockrunner  -->
      <bean id="destinationManager" class="com.mockrunner.jms.DestinationManager"/>

      <bean id="mockQueue" factory-bean="destinationManager" factory-method="createQueue">
      <constructor-arg index="0" value="demoMockRunnerQueue" />
      </bean>
      <bean id="configurationManager" class="com.mockrunner.jms.ConfigurationManager"/>
      <bean id="jmsQueueConnectionFactory" class="com.mockrunner.mock.jms.MockQueueConnectionFactory">
      <constructor-arg index="0" ref="destinationManager" />
      <constructor-arg index="1" ref="configurationManager" />
      </bean>

      <!--  spring -->
      <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
      <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
      </bean>                           

      <jms:listener-container connection-factory="jmsQueueConnectionFactory" >
<jms:listener destination="demoMockRunnerQueue" ref="employeeMessageListener" method="onMessage" />
  </jms:listener-container>

 <bean id="employeeMessageListener"  class="com.jms.timer.demo.EmployeeMessageListener" />

</beans>

EmployeeMessageListener.java

package com.jms.timer.demo;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.listener.SessionAwareMessageListener;

public class EmployeeMessageListener implements SessionAwareMessageListener<TextMessage>{


   @Override
   public void onMessage(TextMessage textMessage, Session session) throws JMSException {

         System.out.println("Message Received "+textMessage.getText());  
   }

}

Spring Unit Test

package com.jms.timer.demo;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mockrunner.mock.jms.MockQueue;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext-mockrunner.xml")
public class MockRunnerJmsSenderTest {

   @Autowired
   private JmsTemplate jmsTemplate;

   @Autowired
   private MockQueue mockQueue;


   @Test
   public void shouldSendMessage()
   {
      jmsTemplate.send(mockQueue, new MessageCreator() {

         @Override
         public Message createMessage(Session session) throws JMSException {
            TextMessage  message = session.createTextMessage();
            message.setText("This is test message from MockRunner");
            return message;
         }
      });

   }

}

 output:

Sep 26, 2014 1:00:34 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/jms/timer/demo/applicationContext-mockrunner.xml]

Sep 26, 2014 1:00:35 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.GenericApplicationContext@77383942: startup date [Fri Sep 26 13:00:35 CDT 2014]; root of context hierarchy

Sep 26, 2014 1:00:35 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@302abefe: defining beans [destinationManager,mockQueue,configurationManager,jmsQueueConnectionFactory,jmsTemplate,org.springframework.jms.listener.DefaultMessageListenerContainer#0,employeeMessageListener,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

Sep 26, 2014 1:00:35 PM org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup start

INFO: Starting beans in phase 2147483647

Message Received This is test message from MockRunner

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
architects ,devops

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}