Zilev av
Zilev av

Reputation: 521

REST Assured with JSON schema validation not working

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:

project structure

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"));

Upvotes: 6

Views: 9393

Answers (2)

Chandresh kumar
Chandresh kumar

Reputation: 17

When you execute your test cases, your complete src/test folder gets compiled and it stores all compiled files inside target/test-classes file, so in your case as you kept your json file inside the src/test/resources, a copy of that file will be created inside the target/test-classes folder and matchesJsonSchemaInClasspath method use that file at the time execution(you can validate by going to target/test-classes folder after you test execution).

Upvotes: 0

Andy Wilkinson
Andy Wilkinson

Reputation: 116281

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.

Upvotes: 10

Related Questions