接口测试返回的json文件中字符串是乱序

问题描述

接口测试中post方式匹配返回信息时显示不匹配, 但是statuscode明明是200, 而且用postman /restclient等工具测出来也是没问题的.

 

根本原因

封装了这么个方法来比对返回的信息, 但是这种方法在转换返回信息时并没有对字符串进行排序. 所以和api文档中的返回值进行匹配时, 显示不匹配.

 1 package com.crewbudgetMO.API.bean;
 2 
 3 import static org.testng.Assert.assertEquals;
 4 
 5 import java.io.ByteArrayOutputStream;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.io.UnsupportedEncodingException;
 9 
10 import com.mashape.unirest.http.HttpResponse;
11 import com.mashape.unirest.http.JsonNode;
12 
13 
14 public class matchResponse {
15     
16     public static void matchPostStatus(HttpResponse<JsonNode> jsonPost, int exp) {
17         int act = jsonPost.getStatus();
18         assertEquals(act, exp);
19     }
20     
21     
22     public static void matchPost (HttpResponse<JsonNode> jsonPost, String filename) {
23         String filePath = String.format("E:\\Tech\\selenium\\projectname\\src\\test\\java\\com\\subfolder\\API\\file\\%s", filename);        
24         InputStream in = jsonPost.getRawBody();
25         ByteArrayOutputStream bos = new ByteArrayOutputStream();    
26         
27         byte[] b = new byte [10240];
28         int length = 0;
29         try {
30             while((length = in.read(b))!=-1) {
31                 bos.write(b,0,length);
32             }
33             in.close();
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37 
38         String act = null;
39         try {
40             act = new String(bos.toByteArray(), "UTF-8");
41         } catch (UnsupportedEncodingException e) {
42             e.printStackTrace();
43         }
44         String exp = convertJson.json2String(filePath);
45         assertEquals(act, exp);    
46     }        
47 }
View Code

 

解决方法

对返回信息的json文件进行排序.

 

转载于:https://www.cnblogs.com/cheese320/p/9014094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值