接口测试返回值验证方式---返回值为json长串

Posted 我是勤劳的搬运工

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口测试返回值验证方式---返回值为json长串相关的知识,希望对你有一定的参考价值。

1.返回值

 

2.验证方式:

public void check200_N(Object obj, int ret_num)
throws UnsupportedEncodingException, Exception {
JSONObject resultJson = JSONObject.fromObject(obj);
Assertions.assertThat(resultJson.getString("retCode")).isEqualTo("200");
Assertions.assertThat(resultJson.getString("retDesc"))
.isEqualTo("操作成功");
Assertions.assertThat(resultJson.containsKey("ret")).isTrue();

JSONArray jsonArray = resultJson.getJSONArray("ret");
Assertions.assertThat(jsonArray.size()).isEqualTo(ret_num);
Iterator<JSONObject> iterator = jsonArray.iterator();
while (iterator.hasNext()) {
JSONObject tmp = iterator.next();
Assertions.assertThat(tmp.getString("id")).isNotEmpty();
Assertions.assertThat(tmp.getString("title")).isNotEmpty();
Assertions.assertThat(tmp.getString("digest")).isNotEmpty();
Assertions.assertThat(tmp.getString("analystName")).isNotEmpty();
Assertions.assertThat(tmp.getString("url")).isNotEmpty();
Assertions.assertThat(tmp.getString("sourceCreateTime"))
.isNotEmpty();
Assertions.assertThat(tmp.getString("replies")).isNotEmpty();
Assertions.assertThat("5,6,4,3,2,1,0").contains(
tmp.getString("type"));
Assertions.assertThat(tmp.getString("analystImage")).isNotEmpty();
Assertions.assertThat(tmp.getString("orderWeight")).isNotEmpty();
String t = tmp.getString("imageList");
if (t != null && (!t.equals("null") && t.length() > 2)) {
JSONArray jsonArray2 = tmp.getJSONArray("imageList");
Iterator<JSONObject> iterator2 = jsonArray2.iterator();
while (iterator2.hasNext()) {
JSONObject tmp2 = iterator2.next();
Assertions.assertThat(tmp2.getString("originWidth"))
.isNotEmpty();
Assertions.assertThat(tmp2.getString("originHeight"))
.isNotEmpty();
Assertions.assertThat(tmp2.getString("originUrl"))
.isNotEmpty();
}
}
}
System.out.println("测试成功");
}

 

以上是关于接口测试返回值验证方式---返回值为json长串的主要内容,如果未能解决你的问题,请参考以下文章

关于接口测试调试的一些总结整理

搞定接口测试变态要求:海量接口返回值对比验证

怎样自定义MyBatis的返回值为JSON格式?

接口测试介绍

Postman测试工具调试接口详细教程向后端发送Json数据并接收返回的Json结果

安卓用httpurlconnection怎么调用接口地址并获取返回值