1

我有一个 post API,我希望使用 JsonSchemaValidator 的方法,因为我希望通过执行断言来验证整个响应而不是选择响应

我试过用

  1. matchesJsonSchemaInClasspath("我的文件名") 和
  2. matchJsonSchema(我的文件对象)

我的 reposne 即将成为现实,方法正在通过,但我的架构文件没有检查或验证

public void directLoginWihSchemaValiadtor(){
    File file = new File("C:/Users/abeey/git/SlingAppWebService/Configurations/JsonSchemaValidator_DirectLogin_AWS.json");
    jsonasmap.put("contactNo", "some number");
    jsonasmap.put("loginType","0");
    jsonasmap.put("appid","2");
    jsonasmap.put("co*****ode","IN");
    JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().
    setValidationConfiguration(ValidationConfiguration.newBuilder().freeze()).freeze();

    given().contentType(ContentType.JSON).body(jsonasmap).when().
    post("https://60i*****.execute-api.us-west-2.amazonaws.com/some-api").
    then().assertThat().
    body(JsonSchemaValidator.matchesJsonSchema(file))).


    log().all();
    jsonasmap.clear();
}

//body(JsonSchemaValidator.matchesJsonSchemaInClasspath("JsonSchemaValidator_DirectLogin_AWS.json").using(jsonSchemaFactory))

我尝试使用 jsonSchemaFactory 来执行此操作,但我没有得到关于设置为 draftversion 的内容或从哪里获取它

我是新手,如果你觉得这个问题太简单了,请多多包涵

4

1 回答 1

2

对于这种情况,我通常会执行以下操作:

  1. 使用模式生成器并为 json 主体创建模式(我使用这个工具json-schema-generator

  2. 将生成的 json 模式放在类路径中(例如 test/resources)

  3. 将此代码用作 REST Assured 测试的一部分:

    .body(matchesJsonSchemaInClasspath("your_schema_name.json"))

如果您想确保架构验证正常工作,您可以编辑架构文件并将任何必填字段的类型更改为其他内容,然后查看您的测试是否会失败。

您可以参考我的这篇文章以查看一些代码示例。

于 2017-07-30T11:06:01.387 回答