【发布时间】:2017-05-08 01:23:48
【问题描述】:
我的任务是为下面的这个方法编写一个单元测试。但是,我对整个模拟和/或使用 PowerMockito 还是很陌生。方法是这样的:
public class Storage {
public static JSONObject addHeader(JSONObject input) throws JSONException {
try {
JSONObject header = new JSONObject();
header.put("update_date", System.currentTime());
header.put("created_date", System.currentTime());
header.put("type", "storage");
input.put("HEADER", header);
}
catch (JSONException e) {
e.printStackTrace();
}
return input;
}
这就是我卡住的地方。在我的测试方法中,我有这行代码。
JSONObject testInput = Whitebox.invokeMethod(Storage, "addHeader", JSONMockTest());
//testInput is always return null so this is where I'm stuck at and as not sure what to do after this
我想验证返回的 JSONMockTest 在运行完该方法后是否包含那些额外的键和值。这可能很简单,但我不知道如何开始。
非常感谢任何帮助。
【问题讨论】:
-
什么是
JSONMockTest?您应该将JSONObject作为方法参数传递。
标签: json unit-testing junit mocking powermockito