3

I have some problems. I've created JSONParser and need to make a unit tests on it. But, if I'm trying to pass a String object with a copy of valid JSON, every time JSONObject, which is part of my Parser have a value of null.

Here the examples.

JSON I'm assigning to the String:

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());
    }
}

My unit-test of JSONParser

@Test
public void testVerbsOnlyParsing() throws JSONException, IOException {
    parser = new JSONParser(verbsJson);
    translations = parser.getTranslations();
    Assert.assertNotNull(translations.getVerbs());
}

And a part of JSONParser, which have a problem of getting data from String during the initialization of JSONObject:

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);
    }

I really don't know what cause the problem (JSONObject object == "null"), could you help me please?

2
  • If you have an exception at runtime, you should always post the full stack trace with your question. Commented Feb 16, 2016 at 21:51
  • The answer for your question is here: stackoverflow.com/a/35675861/3032209 Commented Feb 27, 2016 at 21:51

1 Answer 1

6

TL;DR: add this to your build.gradle file:

testCompile "org.json:json:20140107"

This will replace the stubbed Android library with one that works on the desktop.

See this post for a great explanation https://medium.com/@yair.kukielka/android-unit-tests-explained-219b04dc55b5#.qnsgqo7qf

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.