Zombies
Zombies

Reputation: 25912

JMS dependencies in unit tests

I have a given test which I want to run in JUnit. It has a dependency on a complex service that the code calls using JMS, so while running the JUnit test it will not have access to this. So, given the fact that I need to call this service, what is the best way to stub out this service so that it just returns a hard-coded response when called while the JUnit test is ran?

Right now it uses JNDI to lookup the queue and that works fine now using easymock so that spring initializes without problems. But it also needs to get a response on its reply queue from the stub service now (very important).

Upvotes: 2

Views: 1482

Answers (2)

Péter Török
Péter Török

Reputation: 116306

How is the response supposed to be passed onto the reply queue? I assume it happens by calling a specific callback method somewhere. Who has access to that method?

If the reply queue is passed to the stub service, you can capture it via EasyMock and then invoke methods on it directly. The way to do this is very briefly discussed in the EasyMock documentation (search for "capture"). A simple example:

Capture<Queue> replyQueueCapture = new Capture<Queue>();
...
MessageService stubService = createMock(MessageService.class);
stubService.sendMessage(capture(replyQueueCapture));
...
// run the test which indirectly invokes the stub service
...
Queue replyQueue = replyQueueCapture.getValue();
replyQueue.offer(replyMessage);

Upvotes: 1

whaley
whaley

Reputation: 16265

Have you considered using an embedded ActiveMQ within your unit tests?

http://activemq.apache.org/how-to-unit-test-jms-code.html

Upvotes: 1

Related Questions