Search code examples
javafilecucumberrest-assuredjson-schema-validator

How to resolve JSON Schema validation Error in Rest Assured - io.cucumber.core.exception.CucumberException: Could not convert arguments


I was trying to do the JSON schema validation in Rest Assured framework using the below code by reading the filename loginschema.json from src/test/resources/data folder and was getting CucumberException. Can someone provide their inputs on how to resolve this issue?

**UtilFile:**

import java.io.*;
import java.util.*;
import io.restassured.module.jsv.JsonSchemaValidator;

public void verifyResponseBody(File fileName)  {
String pathName = "src/test/resources/data/"+ fileName +".json";
response.then().assertThat().body(JsonSchemaValidator.matchesJsonSchemaInClasspath(pathName));
}

**Cucumber file:**
Then I see response matches file loginschema


**Common Stepdefintion File:**

@Then("I see response matches file {}")
public void i_see_response_matches_file(File fileName) {
    apiUtil.verifyResponseBody(fileName);
}    

**Error:**     
And I see response matches file loginschema                      # 
test.java.com.stepdefinitions.Common_StepDefintions.i_see_response_matches_file(java.io.File)
  io.cucumber.core.exception.CucumberException: Could not convert arguments for step [I see response matches file {}] defined at 'test.java.com.stepdefinitions.Common_StepDefintions.i_see_response_matches_file(java.io.File)'.

Solution

  • You get this error because you cannot just take an arbitrary sequence of chars from your steps (like you do with {}) and treat it as a File (argument type that your step takes).

    You should change this public void i_see_response_matches_file(File fileName) to this public void i_see_response_matches_file(String fileName) and create File instance from that path internally within a step.

    P.S. - Or you can create custom parameter type and describer conversion there.