Compare JSON response using JUnit and JSONassert

为君一笑 提交于 2020-06-26 06:50:07

问题


I'm relatively new to Java and I'm asking to write test of JSON response server. I found JSONassert very useful but I didn't succeed to write the method getRESTData.

Anybody can help please?

@Test
public void testGetFriends() throws JSONException {
    JSONObject data =  getRESTData("/friends/367.json");
    String expected = "{friends:[{id:123,name:\"Corby Page\"}"
            + ",{id:456,name:\"Solomon Duskis\"}]}";
    JSONAssert.assertEquals(expected, data, false);
}

回答1:


You can get the data as String and pass it into JSONAssert.assertEquals. Converting to JSONObject isn't necessary.

To fetch data from an URL, you can use URL.getContent method:

final String data = new URL("...").getContent();
String expected = "{friends:[{id:123,name:\"Corby Page\"}"
        + ",{id:456,name:\"Solomon Duskis\"}]}";
JSONAssert.assertEquals(expected, data, false);


来源:https://stackoverflow.com/questions/29621046/compare-json-response-using-junit-and-jsonassert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!