Question

In our code we get Json object from ActiveMQ and then decide action and other parameter for action and then return Json object to ActiveMQ. To unit test we have to mock Json object request.

Was it helpful?

Solution

What about mock action instead?

@Mock private Action action;

private ActionDispather subject = new ActionDispather(action);

@Test public void doAction1GivenSomeStateIsTrue() throws Throwable {
    final Message message = new Message();
    message.setState(true);
    //message (json object in your case) population is omitted

    context.checking(new Expectations() {
        {
             oneOf(action).doAction1();
        }
    });

    subject.on(message);
}

@Test public void doAction2GivenSomeStateIsFalse() throws Throwable {
    final Message message = new Message();
    message.setState(false);
    //message (json object in your case) population is omitted

    context.checking(new Expectations() {
        {
             oneOf(action).doAction2();
        }
    });

    subject.on(message);
} 

The json parsing could be handled in Consumer, therefore the action decision test is json agnostic.

public class Consumer {
    private ActionDispatcher dispatcher;       

    public void on(ActiveMQMessage message) {
         Message m = convertFrom(message);
         dispatcher.on(m);
    }
}

Anyway, mock behavior not data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top