Unit testing code that sends JMS messages

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".

Answers


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.


Need Your Help

Using Google maps in Windows Phone 8

windows-phone-7 google-maps windows-phone-8 bing-maps here-api

It's possible using Google maps in Windows Phone 8? The question may seem strange, since the internet is very much a post about it. But in practice, I was faced with real problems.

Detecting IsAjaxRequest() with ASP.NET MVC and JQuery Form Plugin / File Upload

jquery asp.net-mvc forms file-upload

I'm using the JQuery Form plugin to do a file upload on an ASP.NET MVC application. I've learned that since an iframe is used for file uploads (rather than XMLHttpRequest, which isn't possible), the