REST Assured with JSON schema validation not worki

2019-04-09 22:24发布

问题:

I'm working with Spring Boot and REST Assured to test REST APIs. I was trying the example with JSON schema validation but it throws this error:

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

According to documentation, the schema should be located in the classpath. My example schema is located there. Here is my project structure and example schema location:

Here is my code. Without schema validation it works fine.

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

回答1:

Your schema file is in the rest.resource package but you haven't mentioned that when calling matchesJsonSchemaInClasspath. You either need to move the file to the root of the classpath (put it in src/test/resources, for example), or change the string you're passing into matchesJsonSchemaInClasspath.