Json 模式验证使用放心。无法执行

问题描述 投票:0回答:1
public class Response_SchemaValidation 
{
    public static void main(String [] args) throws FileNotFoundException
    {
        System.out.println("This is testing...");
        
        String str ="{\r\n" + 
                "    \"firstname\": \"Sally\",\r\n" + 
                "    \"lastname\": \"Brown\",\r\n" + 
                "    \"totalprice\": 111,\r\n" + 
                "    \"depositpaid\": true,\r\n" + 
                "    \"bookingdates\": {\r\n" + 
                "        \"checkin\": \"2013-02-23\",\r\n" + 
                "        \"checkout\": \"2014-10-23\"\r\n" + 
                "    },\r\n" + 
                "    \"additionalneeds\": \"Breakfast\"\r\n" + 
                "}";
        
        
        //InputStream insp = new FileInputStream("C:\\Users\\ANANDA K R\\eclipse-workspace\\API_Automation_petStore\\Schema_validation_jsonFiles\\Response_SchemaValidation.json");
        //File fl1= new File("C:\\Users\\ANANDA K R\\eclipse-workspace\\API_Automation_petStore\\Schema_validation_jsonFiles\\Response_SchemaValidation.json");
        RequestSpecification reqstBody = 
                RestAssured.given().body(str).header("Content-Type","application/json");
            Response resp = reqstBody.post("https://restful-booker.herokuapp.com/booking");
            //resp.js 
            
            System.out.println(resp.statusCode());
            String str2 = resp.getBody().asString();
            System.out.println("str2 ---"+str2);
            Assert.assertEquals(resp.getBody().asString(), matchesJsonSchemaInClasspath("Response_SchemaValidation.json"));
            
        
    }

}

输出:

Exception in thread "main" java.lang.IllegalArgumentException: Schema to use cannot be null
    at io.restassured.module.jsv.JsonSchemaValidator.validateSchemaIsNotNull(JsonSchemaValidator.java:270)
    at io.restassured.module.jsv.JsonSchemaValidator.access$300(JsonSchemaValidator.java:75)
    at io.restassured.module.jsv.JsonSchemaValidator$JsonSchemaValidatorFactory.create(JsonSchemaValidator.java:281)
    at io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema(JsonSchemaValidator.java:166)
    at io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath(JsonSchemaValidator.java:117)
    at basic_SchemaValidation.Response_SchemaValidation.main(Response_SchemaValidation.java:173)

无法使用 Java 验证模式....请有人帮助推进并完成它吗?

json automation rest-assured json-schema-validator
1个回答
0
投票
        given().
        when().
        get('PutTheApiEndPointHere').
        then().
        log().
        body().
        assertThat().
        statusCode(200).    
        body(JsonSchemaValidator.matchesJsonSchemaInClasspath("ResponseSchema.json"));

使用此网站根据您的响应在线生成架构:https://www.liquid-technologies.com/online-json-to-schema-converter。之后,将架构存储在类路径中,并添加 Json 架构验证器依赖项。链接:https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator

© www.soinside.com 2019 - 2024. All rights reserved.