zoukankan      html  css  js  c++  java
  • 接口自动化增加jsonschema验证

    1. pom.xml 配置

        <dependency>
          <groupId>com.github.fge</groupId>
          <artifactId>json-schema-validator</artifactId>
          <version>2.2.6</version>
        </dependency>

    2. 数据源即 jsonschema文件。

    文件放置于resources目录下,因为测试与线上的数据返回有所不同,所以结合环境隔离

     3. jsonschema和接口返回的校验

    package util;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.github.fge.jackson.JsonNodeReader;
    import com.github.fge.jsonschema.core.report.ProcessingMessage;
    import com.github.fge.jsonschema.core.report.ProcessingReport;
    import com.github.fge.jsonschema.main.JsonSchemaFactory;
    import org.testng.Assert;
    import org.testng.Reporter;
    import java.io.IOException;
    import java.io.InputStream;
    
    /**
     * JsonSchema工具类
     */
    public class jsonSchemaUtil {
        /**
         * 从指定路径读取Schema信息
         *
         * @param filePath Schema路径
         * @return JsonNode型Schema
         * @throws IOException 抛出IO异常
         */
        private static JsonNode readJSONfile(String filePath) throws IOException {
            InputStream stream = jsonSchemaUtil.class.getClassLoader().getResourceAsStream(filePath);
            return new JsonNodeReader().fromInputStream(stream);
        }
    
    
        /**
         * 将Json的String型转JsonNode类型
         *
         * @param str 需要转换的Json String对象
         * @return 转换JsonNode对象
         * @throws IOException 抛出IO异常
         */
        private static JsonNode readJSONStr(String str) throws IOException {
            return new ObjectMapper().readTree(str);
        }
    
        /**
         * 将需要验证的JsonNode 与 JsonSchema标准对象 进行比较
         *
         * @param schema schema标准对象
         * @param data   需要比对的Schema对象
         */
        private static void assertJsonSchema(JsonNode schema, JsonNode data) {
            ProcessingReport report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data);
            if (!report.isSuccess()) {
                for (ProcessingMessage aReport : report) {
                    Reporter.log(aReport.getMessage(), true);
                }
            }
            Assert.assertTrue(report.isSuccess());
        }
    
    
        /**
         * 将需要验证的response 与 JsonSchema标准对象 进行比较
         *
         * @param schemaPath JsonSchema标准的路径
         * @param responseJson   需要验证的Json数据
         * @throws IOException 抛出IO异常
         */
        public static void assertResponseJsonSchema(String schemaPath, String responseJson) throws IOException {
            JsonNode jsonSchema = readJSONfile(schemaPath);
            JsonNode responseJN = readJSONStr(responseJson);
            assertJsonSchema(jsonSchema, responseJN);
        }
    }
  • 相关阅读:
    php date 时间差
    array_merge 和 + 号的的区别
    apache 添加https后导致http无法访问
    php 获取url
    TP5 事务处理
    LeetCode 每日一题 (盛最多水的容器)
    LeetCode 每日一题 (字符串转换整数 (atoi))
    LeetCode 每日一题(5. 最长回文子串)
    LeetCode 每日一题 (3 无重复字符的最长子串)
    LeetCode 每日一题 (两数相加)
  • 原文地址:https://www.cnblogs.com/zqlmmd/p/12074645.html
Copyright © 2011-2022 走看看