Testing a JMS server using Generic Fixture

Its been a while since I posted my last blog on Generic Fixture. In my previous posts I demonstrated how to make use of Generic Fixture to write automated web tests, database tests, testing EJBs etc. Let’s see how to use Generic Fixture for testing a JMS (Java Messaging Service) server. What we are going to write essentially is translation of Java based JMS client into FitNesse using Generic Fixture, which can be used to write acceptance tests to validate almost any type of Java application.

For our demonstration I will be using Apache’s ActiveMQ, the most popular cross platform JMS provider. It’s open source product so please go ahead and download it from ActiveMQ download page. Once downloaded just unzip the installer zip file and start running activemq process from inside activemq-install-dir/bin directory.

First we need to start a JMS server where we shall be sending the JMS messages and getting the response from. ActiceMQ developers have provided a really simple JMS client, server code on this page. Go ahead copy full Server side code from this page and paste it into a file called Server.java on your computer. Save the file and compile it using following javac command:

javac -cp .:geronimo-jms_1.1_spec-1.1.1.jar:activemq-core-5.1.0.jar:geronimo-j2ee-management_1.0_spec-1.0.jar:commons-logging-1.1.jar Server.java

Once compiled just start the JMS Server process using following command

java -cp .:geronimo-jms_1.1_spec-1.1.1.jar:activemq-core-5.1.0.jar:geronimo-j2ee-management_1.0_spec-1.0.jar:commons-logging-1.1.jar Server

Now our JMS server is running, lets test the JMS messaging by writing a simple JMS client on FitNesse. Client program on this activemq page does just that in Java. Now let’s see how are we going to translate this code into FitNesse wiki test script.

Before we start writing FitNesse test script pay close attention to the supplied Client class on the above page. This class is sending a text message to the JMS queue named “client.messages”. However it is getting response from server asynchronously. Response is asynchronously delivered to onMesssage() callback inside the class.

To send a message to the JMS server FitNesse web based test script we must convert this behaviour of the Client class by making it a synchronous call since we cannot have a callback inside FitNesse test script. That can be achieved by sending a message to the JMS queue named “client.messages” and then making a blocking call to get the response in the same thread. Than can be achieved by MessageConsumer.receive(int timeout) method.

Once that is decided it’s really straight forward to write that up in FitNesse. We must include 4 activemq jar files needed to run our client program. We are making use of run-time variable creation feature of Generic Fixture in this script. Here is the complete test script for the JMS client we have discussed above.

!*< Class Path Definition
!path genericfixture.jar
!path geronimo-jms_1.1_spec-1.1.1.jar
!path activemq-core-5.1.0.jar
!path geronimo-j2ee-management_1.0_spec-1.0.jar
!path commons-logging-1.1.jar

!3 JMS Setup
!| Generic Fixture | org.apache.activemq.ActiveMQConnectionFactory | tcp://localhost:61616 |
| connection=createConnection |
| connection=.start |
| session=connection=.createSession | false | javax.jms.Session.AUTO_ACKNOWLEDGE |

!3 Send a message and wait for 5 sec to get a response
!| Generic Fixture | session= |
| adminQueue=createQueue | client.messages |
| producer=createProducer | adminQueue= |
| tempDest=createTemporaryQueue |
| consumer=createConsumer | tempDest= |
| txtMessage=createTextMessage |
| txtMessage=.setText | MyProtocolMessage |
| txtMessage=.setJMSReplyTo | tempDest= |
| txtMessage=.setJMSCorrelationID | 100 |
| producer=.send | txtMessage= |
| response=consumer=.receive | 5000 |
| response=.getText | | I recognize your protocol message |

Go ahead and give it a try. This test script if executed correctly should result in 1 green cell in last cell of the 2nd table by matching the server’s response with the expected value in the table.


One thought on “Testing a JMS server using Generic Fixture

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s