UnitTest JSONObject 显示为空 [英] UnitTest JSONObject shows null
问题描述
我遇到了与 JSONObject 相关的问题.
I have got a problem connected with JSONObject.
@Test
public void toUrlTest() throws JSONException {
String url;
JSONObject json = new JSONObject();
json.put"id", 1);
json.put("email", "test@hotmail.com");
url = JSONParser.toURLString(json);
assertEquals("id=1&email=test@hotmail.com", url);
}
问题是当我调试这个测试时,它显示没有任何东西被放入 json 对象.
The problem is when I am debugging this test, it shows that nothing is put to json object.
json={org.json.JSONObject@826} "null"
json={org.json.JSONObject@826} "null"
我检查了所有内容,但不知道为什么会发生这种情况.JSONObject 在应用程序中运行良好.它只在测试时发生.
I checked everything and I got no clue why this happen. JSONObject works fine in app. It only happens while testing.
附注.我在 build.gradle 中添加了这个
PS. I have added in build.gradle this
testOptions {
unitTests.returnDefaultValues = true
}
推荐答案
TL;DR 版本 Yair Kukielka 的回应...(谢谢!)
TL;DR version of Yair Kukielka's response... (thanks!)
按照
testImplementation "org.json:json:20140107"
这会将存根的 Android 库替换为可在桌面上运行的库.
This will replace the stubbed Android library with one that works on the desktop.
这篇关于UnitTest JSONObject 显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!