DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • That Can Not Be Tested!: Spring Cache and Retry
  • Camel Route - Unit Testing

Trending

  • APIs for Logistics Orchestration: Designing for Compliance, Exceptions, and Edge Cases
  • Why Rate Limiting Matters in Istio and How to Implement It
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  1. DZone
  2. Coding
  3. Frameworks
  4. MockRunner with JMS Spring Unit Test

MockRunner with JMS Spring Unit Test

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

By 
Upender Chinthala user avatar
Upender Chinthala
·
Oct. 06, 14 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
58.1K Views

Join the DZone community and get the full member experience.

Join For Free

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:



  • Maven 
  • Spring 
  • MockRunner 
  • STS IDE 


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

unit test Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • That Can Not Be Tested!: Spring Cache and Retry
  • Camel Route - Unit Testing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!