REST Assured JSON 架构验证不起作用

REST Assured with JSON schema validation not working

我正在使用 Spring Boot 和 REST Assured 来测试 REST API。我正在尝试使用 JSON 模式验证的示例,但它抛出了这个错误:

java.lang.IllegalArgumentException: Schema to use cannot be null

根据文档,架构应为 located in the classpath。我的示例模式位于那里。这是我的项目结构和示例架构位置:

这是我的代码。没有模式验证它工作正常。

given().
    contentType("application/json").
when().
    get("http://myExample/users").
then().
    assertThat().body(matchesJsonSchemaInClasspath("example_schema.json"));

您的模式文件在 rest.resource 包中,但您在调用 matchesJsonSchemaInClasspath 时没有提及。您需要将文件移动到类路径的根目录(例如,将其放在 src/test/resources 中),或者更改要传递到 matchesJsonSchemaInClasspath.

中的字符串

当您执行测试用例时,您的完整 src/test 文件夹会被编译并将所有已编译的文件存储在 target/test-classes 文件中,因此在您保留 json 文件的情况下在 src/test/resources 中,将在 target/test-classes 文件夹中创建该文件的副本,并且 matchesJsonSchemaInClasspath 方法在执行时使用该文件(您可以在之后转到 target/test-classes 文件夹进行验证你测试执行)。