美文网首页软件测试学习之路
Jmeter 校验response报文格式是否为JSON数据

Jmeter 校验response报文格式是否为JSON数据

作者: 乘风破浪的姐姐 | 来源:发表于2018-07-03 11:21 被阅读199次

    Jmeter 接口测试中,通常要校验请求报文返回的结果,对于返回的结果格式,有的是xml,有的是json。这里对返回报文是否为json格式进行校验。

    以下是接口返回报文:

    {
        "accidentNo": "AN1111",
        "auditReport": {
            "auditRuleTriggers": [{
                "actualValue": "    次数:\n            137\n",
                "auditScore": 10,
                "itemInfoList": [],
                "itemName": "本碰撞点:1",
                "redLineType": "00",
                "ruleName": "历史出险碰撞点相同",
                "ruleNo": "A102010046",
                "ruleType": "02",
                "ruleValue": "    次数:\n        6\n"
            },
            {
                "actualValue": "        24.04\n",
                "auditScore": 20,
                "itemInfoList": [{
                    "claimItemUniqueId": null,
                    "itemName": "xxxx栅",
                    "itemType": "01",
                    "materialFeeAfterDiscount": null,
                    "quantity": 1,
                    "removeLaborFeeDiscount": 24.04,
                }],
                "itemName": "xxxx栅\n",
                "redLineType": "00",
                "ruleName": "单项维修金额超过系统价",
                "ruleNo": "A101050009",
                "ruleType": "01",
                "ruleValue": "        24.00\n"
            }]
        },
        "claimUniqueId": "CN070210",
        "interfaceCode": "ClaimPush",
        "message": "success",
        "resultCode": "000"
    }
    

    校验上述报文格式,详细步骤:
    1、使用在线json schema生成工具自动生成上述报文的schema
    地址:https://jsonschema.net/。将生成的内容保存为responseSchema.json文件,原返回结果保存为auditReport.json

    {
      "type": "object", 
      "definitions": {}, 
      "properties": {
        "accidentNo": {
          "type": "string"
        }, 
        "auditReport": {
          "type": "object", 
          "properties": {
            "auditRuleTriggers": {
              "type": "array", 
              "items": {
                "type": "object", 
                "properties": {
                  "actualValue": {
                    "type": "string"
                  }, 
                  "auditScore": {
                    "type": "integer"
                  }, 
                  "itemInfoList": {
                    "type": "array"
                  }, 
                  "itemName": {
                    "type": "string"
                  }, 
                  "redLineType": {
                    "type": "string"
                  }, 
                  "ruleName": {
                    "type": "string"
                  }, 
                  "ruleNo": {
                    "type": "string"
                  }, 
                  "ruleType": {
                    "type": "string"
                  }, 
                  "ruleValue": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }, 
        "claimUniqueId": {
          "type": "string"
        }, 
        "interfaceCode": {
          "type": "string"
        }, 
        "message": {
          "type": "string"
        }, 
        "resultCode": {
          "type": "string"
        }
      }
    }
    

    2、新建maven工程,将上述两个文件放置main录的resources下,并导入包:json-schema-validator、fastjson

     <!-- 用于校验json报文格式-->
          <dependency>
              <groupId>com.github.fge</groupId>
              <artifactId>json-schema-validator</artifactId>
              <version>2.2.6</version>
          </dependency>
         <!-- 用于json报文的转换-->
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.40</version>
         </dependency>
    

    3、新增类 JsonSchemaValidator,代码如下

    package com.sc;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import com.github.fge.jackson.JsonLoader;
    import com.github.fge.jsonschema.core.exceptions.ProcessingException;
    import com.github.fge.jsonschema.core.report.ProcessingReport;
    import com.github.fge.jsonschema.main.JsonSchema;
    import com.github.fge.jsonschema.main.JsonSchemaFactory;
    import java.io.IOException;
    
    
    public class JsonSchemaValidator {
        private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        public static ProcessingReport validatorSchema(String response) throws IOException, ProcessingException {
           JsonSchema jsonSchema = factory.getJsonSchema(JsonLoader.fromResource("/responseSchema.json"));
           JsonNode jsonNode = JsonLoader.fromString(response);
           ProcessingReport report = jsonSchema.validate(jsonNode);
           return  report;
        }
    }
    
     /**
         * 在解析响应前,使用JsonSchema对响应的格式进行校验,如果校验失败,则直接返回 fail
         *
         * @return 校验结果
         */
    public  static boolean checkJsonFormat() throws IOException, ProcessingException {
            ProcessingReport report = JsonSchemaValidator.validatorSchema(response);
            return  report.isSuccess();
        }
    

    说明:
    1)、方法validatorSchema
    先通过JsonSchemaFactory.byDefault()获取到JsonSchemaFactory
    加载JsonSchema,即模板
    将接口的返回结果转换成JsonNode
    最后使用加载的JsonSchema校验JsonNode

    2)、方法checkJsonFormat
    使用validatorSchema的返回结果,调用ProcessingReport 的isSuccess方法,返回校验结果

    4、新增测试类,校验auditReport.json格式

    public class Test{
        public static void main(String[] args) throws IOException, ProcessingException {
            String response = FileUtils.readFileToString(new File("./src/main/resources/auditReport.json"),"utf-8");
            JsonResponse jsonResponse = new JsonResponse(response);
            boolean flag = JsonSchemaValidator.checkJsonFormat(response);
            if(flag){
                System.out.println("响应结果格式符合要求;");
            }else{
                System.out.println("响应结果格式不符合要求!!;");
            }
    }
    }
    

    运行,结果


    image.png

    ok,准备工作完成。将上述工程打包,引入到jmeter,直接在beanshell中调用上述方法即可。

    相关文章

      网友评论

        本文标题:Jmeter 校验response报文格式是否为JSON数据

        本文链接:https://www.haomeiwen.com/subject/zqsduftx.html