【问题标题】:How to use Jmock to produce mock Json request?如何使用 Jmock 生成模拟 Json 请求?
【发布时间】:2014-03-14 12:45:06
【问题描述】:

在我们的代码中,我们从 ActiveMQ 中获取 Json 对象,然后决定 action 和其他 action 参数,然后将 Json 对象返回给 ActiveMQ。要进行单元测试,我们必须模拟 Json 对象请求。

【问题讨论】:

  • 你想测试什么? json解析还是动作决策?
  • 我们要测试行动决策。

标签: mocking jmock


【解决方案1】:

那么模拟动作呢?

@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);
} 

json 解析可以在 Consumer 中处理,因此动作决策测试与 json 无关。

public class Consumer {
    private ActionDispatcher dispatcher;       

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

无论如何,模拟行为而不是数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多