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)'.
CodePudding user response:
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.
CodePudding user response:
Alexey already provided the reason for exception. Here I am showing you how to write custom parameter type for cucumber expression.
Feature:
Scenario Outline: File name
Then I see response matches file <loginschema>
Examples:
| loginschema |
| C:\\Users\\Nandan\\EclipseWorkspace\\APIPractice\\PayLoads\\Response.json |
StepDefinition:
/*Converting the output parameter type to File*/
@ParameterType(".*")
public File file(String path) {
return new File(path);
}
@Then("I see response matches file {file}")
public void i_see_response_matches_file(File fileName) {
System.out.println(fileName);
}
Output:
C:\Users\Nandan\EclipseWorkspace\APIPractice\PayLoads\Response.json