samedi 21 février 2015

Getting java.lang.RuntimeException: Stub when testing with JSONObject in Robolectric

I am trying to test a method that parses a JSONObject. I am using Robolectric and am getting a java.lang.RuntimeException: Stub when doing so. The exception is thrown when trying to create the new JSONObject.


Why is it giving me this error? Does this have to do because I am accessing a static method?



@Test
public void parseGameJsonShouldReturnNotNullGame() throws Exception {
JSONObject obj = new JSONObject("{" +
"_id: \"538e72fe6a15ceec142a71c8\", " +
"name: \"World of Warcraft\", " +
"coverPhoto: [\"world_of_warcraft_pc.jpg\"], " +
"platform: \"pc\", " +
"genre: \"mmorpg\" " +
"}");
Game game = JsonManager.parseGameJson(obj);
assertNotNull(game);
}

public static Game parseGameJson(JSONObject obj) {
try {
String id = "";
if (obj.has(GlobalVars.KEY_ID)) {
id = obj.getString(GlobalVars.KEY_ID);
}

String name = "";
if (obj.has(GlobalVars.KEY_NAME)) {
name = obj.getString(GlobalVars.KEY_NAME);
}

String genre = "";
if (obj.has(GlobalVars.KEY_GENRE)) {
genre = obj.getString(GlobalVars.KEY_GENRE);
}

String platform = "";
if (obj.has(GlobalVars.KEY_PLATFORM)) {
platform = obj.getString(GlobalVars.KEY_PLATFORM);
}

String coverPhoto = "";
if (obj.has(GlobalVars.KEY_COVER_PHOTO)) {
JSONArray coverPhotoArray = obj.getJSONArray(GlobalVars.KEY_COVER_PHOTO);

for (int c = 0; c < coverPhotoArray.length(); c++) {
if (c == 0) {
coverPhoto = coverPhotoArray.getString(c);
}
else {
coverPhoto = coverPhoto + ", " + coverPhotoArray.getString(c);
}
}
}

return new Game(id, name, genre, platform, coverPhoto);
}
catch (JSONException e) { e.printStackTrace(); }

return null;
}

Aucun commentaire:

Enregistrer un commentaire