4

I need to compare my api tests response against an external JSON file.

Seem to be seeing the following exception: 'java.lang.IllegalArgumentException: Schema to use cannot be null' when executing my api test.

My Test:

@Test
public void validateAgainstJsonFile() {
    given()
            .spec(footballCompetitions_requestSpecification)
            .when().get(EndPoint.AREAS + "2014")
            .then()
            .assertThat()
            .body(matchesJsonSchemaInClasspath("footballCompetitions.json"));
}

My projects structure looks like the following:

https://ibb.co/M62rHyF

Stack trace:

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 apiTests.footballData.CompetitionsGetTest.validateAgainstJsonFile(CompetitionsGetTest.java:195)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
SamP
  • 417
  • 5
  • 24
  • can u paste your stack trace? – Tim Jan 08 '19 at 13:51
  • could do you check that the response of your endpoint request and the json file are not null? – cralfaro Jan 08 '19 at 13:55
  • I have the same issue i tried by going with this [https://stackoverflow.com/questions/33424594/rest-assured-with-json-schema-validation-not-working](https://stackoverflow.com/questions/33424594/rest-assured-with-json-schema-validation-not-working) – jitendra Jan 08 '19 at 14:09
  • I tried the following, didnt seem to work, I have attached my stack trace to the master ticket. – SamP Jan 08 '19 at 15:46

6 Answers6

4

Try creating a folder under test called resources and moving there the file footballCompetitions.json something like: /test/resources/footballCompetitions.json

  test
    └───  java
    └───  resources
         └─── footballCompetitions.json

Then .body(matchesJsonSchemaInClasspath("footballCompetitions.json")); should work fine.

juanlumn
  • 6,155
  • 2
  • 30
  • 39
0

Are you sure that the file path is right? Move this file to: /test/resources/

or use absolute path like below: .body(matchesJsonSchemaInClasspath("/src/test/java/apitests/footballData/footballCompetitions.json"));

0

In my case, it was my mistake, i.e I did not save the file in the proper format. The file was saved as xyz.json.txt. Please ensure that the file is saved in the correct format. It will work.

iammallikarjuna
  • 171
  • 1
  • 6
0

Save json file in Resource Folder and it will work

enter image description here

String Jsonpaths="Examples.json";

assertThat(RESPONSEBODY,matchesJsonSchemaInClasspath(Jsonpaths));

0

Place the JSON-Schema file under Target classPath:

enter image description here

enzo
  • 9,861
  • 3
  • 15
  • 38
-1

This error is related to your schema which you are inserting into

matchesJsonSchemaInClasspath()

First, try to check that your schema JSON may have some null value. You can remove those from the JSON schema file and can check it again.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36