我认为当我试图使用Mockito
来窥探JSONObject
时出现问题。下面是从单元测试方法代码:模拟JSONObject无法检索已添加的值
JSONObject json = new JSONObject();
json.put("token","value");
JSONObject spyJson = Mockito.spy(JSONObject.class);
PowerMockito.whenNew(JSONObject.class).withAnyArguments().thenReturn(spyJson);
service.getToken(json.toString());
这里的方法getToken()
的beggining:我已经添加
public LoginResponseData getToken(String response) throws JSONException {
JSONObject resJson = new JSONObject(response); //resJson = {} here
//do stuff
}
在我的build.gradle: testCompile 'org.json:json:20140107'
对我也适用,'org.json:json:20160212' – mszymborski
另外,你可能想'spy''json',而不是'JSONObject.class'。 –