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

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中调用上述方法即可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,600评论 1 92
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,196评论 6 13
  • “大海好黑啊” “也不是,天亮后便会很美的” 突然想起星爷《喜剧之王》中的这句台词,这是男女主角坐在漆黑的海边,看...
    左行右可阅读 212评论 0 1
  • 2017年4月9日今天分享一个奇迹,我一直想看看樱花,前两年去青岛没赶上看樱花的花期,我这两天盘算着再去一次青岛呢...
    传奇姐姐阅读 189评论 0 0