San*_*rog 3 android json unit-testing
我有一些问题.我已经创建了JSONParser,需要对它进行单元测试.但是,如果我尝试使用有效JSON的副本传递String对象,则每次JSONObject(我的Parser的一部分)都具有null值.
这里的例子.
我正在分配给字符串的JSON:
public class JSONParserTest {
private JSONParser parser;
private Translations translations;
private String nounsAndAdjectivesJson = "{\"head\":{},\"def\":[{\"text\":\"house\",\"pos\":\"noun\",\"ts\":\"ha?s\",\"tr\":[{\"text\":\"???\",\"pos\":\"???????????????\",\"gen\":\"?\",\"syn\":[{\"text\":\"?????\",\"pos\":\"???????????????\",\"gen\":\"?\"},{\"text\":\"????\",\"pos\":\"???????????????\",\"gen\":\"?\"}],\"mean\":[{\"text\":\"home\"},{\"text\":\"cottage\"}],\"ex\":[{\"text\":\"white house\",\"tr\":[{\"text\":\"????? ???\"}]},{\"text\":\"wooden house\",\"tr\":[{\"text\":\"?????????? ?????\"}]}]},{\"text\":\"?????????\",\"pos\":\"???????????????\",\"gen\":\"??\",\"syn\":[{\"text\":\"??????\",\"pos\":\"???????????????\",\"gen\":\"??\"}],\"mean\":[{\"text\":\"room\"},{\"text\":\"building\"}],\"ex\":[{\"text\":\"house of parliament\",\"tr\":[{\"text\":\"?????? ??????????\"}]}]},{\"text\":\"?????????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"hotel\"}]},{\"text\":\"?????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"family\"}]},{\"text\":\"?????????\",\"pos\":\"???????????????\",\"gen\":\"??\",\"mean\":[{\"text\":\"farm\"}]},{\"text\":\"?????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"theatre\"}]},{\"text\":\"??????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"chamber\"}],\"ex\":[{\"text\":\"house of representatives\",\"tr\":[{\"text\":\"?????? ??????????????\"}]}]},{\"text\":\"?????\",\"pos\":\"???????????????\",\"gen\":\"??\",\"syn\":[{\"text\":\"??????\",\"pos\":\"???????????????\",\"gen\":\"??\"}],\"mean\":[{\"text\":\"housing\"}],\"ex\":[{\"text\":\"safe houses\",\"tr\":[{\"text\":\"?????????? ?????\"}]},{\"text\":\"traditional house\",\"tr\":[{\"text\":\"???????????? ??????\"}]}]},{\"text\":\"???\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"kind\"}]},{\"text\":\"?????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"cutting\"}]},{\"text\":\"????????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"dynasty\"}]},{\"text\":\"???????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"audience\"}]},{\"text\":\"?????\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"market\"}]}]},{\"text\":\"house\",\"pos\":\"adjective\",\"ts\":\"ha?s\",\"tr\":[{\"text\":\"????????\",\"pos\":\"??????????????\",\"mean\":[{\"text\":\"home\"}],\"ex\":[{\"text\":\"house arrest\",\"tr\":[{\"text\":\"???????? ?????\"}]}]},{\"text\":\"???????\",\"pos\":\"??????????????\",\"ex\":[{\"text\":\"house mouse\",\"tr\":[{\"text\":\"??????? ????\"}]}]},{\"text\":\"?????????\",\"pos\":\"??????????????\",\"mean\":[{\"text\":\"room\"}]}]},{\"text\":\"house\",\"pos\":\"verb\",\"ts\":\"ha?s\",\"tr\":[{\"text\":\"?????????????????\",\"pos\":\"??????\",\"asp\":\"?????\"},{\"text\":\"???????????\",\"pos\":\"??????\",\"asp\":\"?????\",\"mean\":[{\"text\":\"host\"}]},{\"text\":\"????\",\"pos\":\"??????\",\"asp\":\"?????\",\"mean\":[{\"text\":\"live\"}]},{\"text\":\"???????\",\"pos\":\"??????\",\"asp\":\"?????\",\"mean\":[{\"text\":\"accommodate\"}]},{\"text\":\"????????\",\"pos\":\"??????\",\"asp\":\"???\",\"syn\":[{\"text\":\"????????\",\"pos\":\"??????\",\"asp\":\"???\"}],\"mean\":[{\"text\":\"settle\"},{\"text\":\"shelter\"}]},{\"text\":\"????????\",\"pos\":\"??????\",\"asp\":\"?????\",\"mean\":[{\"text\":\"place\"}]}]}]}";
private String verbsJson = "{\"head\":{},\"def\":[{\"text\":\"peek\",\"pos\":\"verb\",\"ts\":\"pi?k\",\"tr\":[{\"text\":\"?????????\",\"pos\":\"??????\",\"asp\":\"???\",\"syn\":[{\"text\":\"?????????\",\"pos\":\"??????\",\"asp\":\"???\"},{\"text\":\"???????????\",\"pos\":\"??????\",\"asp\":\"?????\"}],\"mean\":[{\"text\":\"glance\"},{\"text\":\"look\"}],\"ex\":[{\"text\":\"peek inside\",\"tr\":[{\"text\":\"????????? ??????\"}]}]},{\"text\":\"?????????\",\"pos\":\"??????\",\"asp\":\"???\",\"syn\":[{\"text\":\"???????????\",\"pos\":\"??????\",\"asp\":\"?????\"},{\"text\":\"????????????\",\"pos\":\"??????\",\"asp\":\"?????\"}],\"mean\":[{\"text\":\"look\"},{\"text\":\"peep\"}]}]},{\"text\":\"peek\",\"pos\":\"noun\",\"ts\":\"pi?k\",\"tr\":[{\"text\":\"??????? ??????\",\"pos\":\"???????????????\",\"mean\":[{\"text\":\"quick look\"}]},{\"text\":\"?????? ????????\",\"pos\":\"???????????????\"},{\"text\":\"???\",\"pos\":\"???????????????\",\"gen\":\"?\",\"mean\":[{\"text\":\"peak\"}]}]}]}";
@Before
public void setUp() throws Exception {
translations = new Translations();
}
@Test
public void testVerbsOnlyParsing() throws JSONException{
JSONObject jsonObject = new JSONObject(verbsJson);
parser = new JSONParser(jsonObject);
translations = parser.getTranslations();
Assert.assertNotNull(translations.getVerbs());
}
}
Run Code Online (Sandbox Code Playgroud)
我对JSONParser的单元测试
@Test
public void testVerbsOnlyParsing() throws JSONException, IOException {
parser = new JSONParser(verbsJson);
translations = parser.getTranslations();
Assert.assertNotNull(translations.getVerbs());
}
Run Code Online (Sandbox Code Playgroud)
还是JSONParser的一部分,它在JSONObject初始化期间遇到从String获取数据的问题:
public class JSONParser {
private Translations translations;
public JSONParser(String response) throws JSONException {
translations = new Translations();
parseJSON(response);
}
private void parseJSON(String response) throws JSONException {
JSONObject object = new JSONObject(response); //object == "null"
JSONArray arrayOfDefinitions = getArray(object, DEFINITION);
parseArray(arrayOfDefinitions, TRANSLATIONS);
}
Run Code Online (Sandbox Code Playgroud)
我真的不知道导致问题的原因(JSONObject object =="null"),你能帮帮我吗?
TL; DR:将其添加到您的build.gradle文件中:
testCompile "org.json:json:20140107"
Run Code Online (Sandbox Code Playgroud)
这将取代存根的Android库与在桌面上工作的库.
请参阅此帖子以获得精彩解释https://medium.com/@yair.kukielka/android-unit-tests-explained-219b04dc55b5#.qnsgqo7qf