使用JUnit和JSONassert比较JSON响应 [英] Compare JSON response using JUnit and JSONassert

查看:140
本文介绍了使用JUnit和JSONassert比较JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我想编写JSON响应服务器的测试. 我发现 JSONassert 非常有用,但是我没有成功编写方法getRESTData.

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.

有人可以帮忙吗?

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

推荐答案

您可以将数据获取为String并将其传递到JSONAssert.assertEquals. 无需转换为JSONObject.

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

要从URL获取数据,可以使用URL.getContent方法:

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

这篇关于使用JUnit和JSONassert比较JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆