我的任务是为这个方法编写一个单元测试。不过,我对整个嘲笑和/或使用PowerMockito还是比较陌生的。这是方法:如何使用PowerMockito模拟此JSONObject?
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'作为方法参数。 –