Jmeter 接口測試中,通常要校驗請求報文返回的結果,對于返回的結果格式,有的是xml,有的是json。這里對返回報文是否為json格式進行校驗。
以下是接口返回報文:
{
"accidentNo": "AN1111",
"auditReport": {
"auditRuleTriggers": [{
"actualValue": " 次數(shù):\n 137\n",
"auditScore": 10,
"itemInfoList": [],
"itemName": "本碰撞點:1",
"redLineType": "00",
"ruleName": "歷史出險碰撞點相同",
"ruleNo": "A102010046",
"ruleType": "02",
"ruleValue": " 次數(shù):\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": "單項維修金額超過系統(tǒng)價",
"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中調用上述方法即可。