i have a class that after it does some stuff, sends a JMS message. i'd like to unit test the "stuff", but not necessarily the sending of the message. when i run my test, the "stuff" green bars, but then fails when sending the message (it should, the app server is not running). what is the best way to do this, is it to mock the message queue, if so, how is that done i am using Spring, and "jmsTemplate" is injected, along with "queue". thanks
You can inject a mocked jmsTemplate.
Assuming easymock, something like
JmsTemplate mockTemplate = createMock(JmsTemplate.class)
That would do the trick.
The simplest answer I would use is to stub out the message sending functionality. For example, if you have this:
public class SomeClass {
public void doit() {
//do some stuff
sendMessage( /*some parameters*/);
}
public void sendMessage( /*some parameters*/ ) {
//jms stuff
}
}
Then I would write a test that obscures the sendMessage behavior. For example:
@Test
public void testRealWorkWithoutSendingMessage() {
SomeClass thing = new SomeClass() {
@Override
public void sendMessage( /*some parameters*/ ) { /*do nothing*/ }
}
thing.doit();
assertThat( "Good stuff happened", x, is( y ) );
}
If the amount of code that is stubbed out or obscured is substantial, I would not use an anonymous inner class but just a "normal" inner class.
i love the inner class idea, wich i would have thought of that!
for future reference, in case i need to use easymock, once i have the mockTemplate, which i assume is in the test case, do i call the setJmsTemplate() myself, using the mockTemplate, is that how it gets injected(manually)?
thanks
Another option is MockRunner which provides mock environments for JDBC, JMS, JSP, JCA and EJB. This allows you to define the queues/topics just like you would in the "real" case and simply send the message.