質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top