Testing JMS Applications
Distributed applications are hard to test. It's especially hard to test applications that might have remote behaviors. The good news is that with Spring, you don't have to actually invoke JMS to retrieve your messages. In this example, you'll learn how to create a stream of messages in the context that can save you the headache of actually creating the JMS infrastructure in every test case.
How do I do that?
You're going to create a series of messages in the Spring context. Your applications will draw these messages from an array list, instead of actually going across the wire and physically retrieving a JMS message. True, you won't be testing JMS, but you will be able to test the elements of your application that depend on JMS. You'll need to do the following:
Implement the interface that you use to deal with JMS. In our case, we'll subclass the façade.
In the new test implementation, draw messages from an array list, rather than from JMS.
Populate this array list data within the Spring context.
First, let's create a subclass of the façade, for test purposes only (Example 8-16). You'll need to modify the JMS method on the façade. Draw your messages from an array list.
Example 8-16. JMSTestRentABike.java
private List testBikes; public List getTestBikes( ) { return testBikes; } public void setTestBikes(List testBikes) { this.testBikes = testBikes; } int curBike = 0; public Bike getNewBikeFromQueue( ) { try { ActiveMQTextMessage m = (ActiveMQTextMessage)testBikes.get(curBike); ...