使用Rest Assured进行JSON Schema验证失败,请求技术协助
解决Rest Assured JSON Schema验证「Schema to use cannot be null」异常
问题场景
使用Java结合Rest Assured做JSON Schema验证时,抛出Schema to use cannot be null异常,代码及错误信息如下:
原代码
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)
原因分析
- Schema文件路径错误:
matchesJsonSchemaInClasspath方法仅在**类路径(classpath)**下查找文件,原代码中Schema文件放在项目自定义目录,未加入类路径,导致Rest Assured找不到文件,返回null的Schema。 - 验证方法用法错误:用
Assert.assertEquals将响应字符串与matchesJsonSchemaInClasspath返回的验证器对象直接比较,这是错误的用法,Rest Assured的Schema验证需通过链式断言调用。
解决方案
1. 调整Schema文件位置
- Maven/Gradle项目:将
Response_SchemaValidation.json放到src/main/resources目录下(该目录默认属于类路径)。 - 普通Java项目:将Schema文件所在目录添加到项目的类路径配置中。
2. 修正验证代码
使用Rest Assured的链式断言语法执行Schema验证,替换错误的Assert.assertEquals写法:
import io.restassured.RestAssured; import io.restassured.module.jsv.JsonSchemaValidator; import static io.restassured.RestAssured.given; public class Response_SchemaValidation { public static void main(String[] args) { 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" + "}"; RestAssured.baseURI = "https://restful-booker.herokuapp.com"; // 发送请求并执行Schema验证 given() .header("Content-Type", "application/json") .body(str) .when() .post("/booking") .then() .statusCode(200) // 先验证响应状态码 .assertThat() .body(JsonSchemaValidator.matchesJsonSchemaInClasspath("Response_SchemaValidation.json")); // 验证JSON Schema System.out.println("JSON Schema验证通过"); } }
3. 确保Schema文件内容正确
对应restful-booker创建booking的响应,示例Schema文件内容如下:
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "bookingid": { "type": "integer" }, "booking": { "type": "object", "properties": { "firstname": { "type": "string" }, "lastname": { "type": "string" }, "totalprice": { "type": "integer" }, "depositpaid": { "type": "boolean" }, "bookingdates": { "type": "object", "properties": { "checkin": { "type": "string" }, "checkout": { "type": "string" } }, "required": ["checkin", "checkout"] }, "additionalneeds": { "type": "string" } }, "required": ["firstname", "lastname", "totalprice", "depositpaid", "bookingdates"] } }, "required": ["bookingid", "booking"] }
关键注意点
- 类路径优先级:如果Schema文件在子目录,路径要写成
subdir/Response_SchemaValidation.json,保证matchesJsonSchemaInClasspath能正确定位。 - 依赖检查:确保项目中已引入Rest Assured的JSON Schema验证依赖(Maven需添加
io.restassured:json-schema-validator依赖)。
内容的提问来源于stack exchange,提问作者Reddy Anand
相关产品推荐
相关产品推荐

