In REST Assured, how do I solve UnrecognizedPropertyException when mapping response to an object?


In REST Assured, how do I solve UnrecognizedPropertyException when mapping response to an object?



I am new to REST Assured API testing, I have already added jackson-databind, jackson-annotations and jackson-core All 2.9.6 versions to the library.



Below is my code:


public class SerializeAndDeserialize {

@Test
public void RegistrationSuccessful()
{

RestAssured.baseURI ="http://restapi.demoqa.com/customer";
RequestSpecification request = RestAssured.given();

JSONObject requestParams = new JSONObject();
requestParams.put("FirstName", "Virkjdender"); // Cast
requestParams.put("LastName", "Scklxjingh");
requestParams.put("UserName", "63userf2d3cn,mxd2011");
requestParams.put("Password", "passwors000mxzmd1");
requestParams.put("Email", "ed26dfdasdf39@gmail.com");


request.body(requestParams.toJSONString());
Response response = request.post("/register");

ResponseBody body = response.getBody();
System.out.println(response.body().asString());
System.out.println(response.contentType());
System.out.println(response.getStatusCode());

if(response.statusCode() == 200)
{
// Deserialize the Response body into RegistrationFailureResponse
RegistrationFailureResponse responseBody = body.as(RegistrationFailureResponse.class);
Assert.assertEquals("User already exists", responseBody.FaultId);
Assert.assertEquals("FAULT_USER_ALREADY_EXISTS", responseBody.fault);
}
else if (response.statusCode() == 201)
{
// Deserialize the Response body into RegistrationSuccessResponse
RegistrationSuccessResponse responseBody = body.as(RegistrationSuccessResponse.class);
Assert.assertEquals("OPERATION_SUCCESS", responseBody.SuccessCode);
Assert.assertEquals("Operation completed successfully", responseBody.Message);
}
}

}



And the related classes are below:


public class RegistrationFailureResponse {
@JsonIgnoreProperties(ignoreUnknown = true)

String FaultId;
String fault;

}



Another one :


public class RegistrationSuccessResponse {
@JsonIgnoreProperties(ignoreUnknown = true)


public String SuccessCode;
public String Message;
}



When I try to run the above code tyhrough Testng.xml file , I get following error:



com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "FaultId" (class RegistrationFailureResponse), not marked as ignorable (0 known properties: ])
at [Source: (String)"{
"FaultId": "User already exists",
"fault": "FAULT_USER_ALREADY_EXISTS"
}"; line: 2, column: 17] (through reference chain: RegistrationFailureResponse["FaultId"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3042)
at com.fasterxml.jackson.databind.ObjectMapper$readValue$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
......



Please help, I googled the solutions but none of it is related to what I am facing here





Possible duplicate of Jackson with JSON: Unrecognized field, not marked as ignorable
– vahdet
Jun 26 at 18:41




1 Answer
1



You should actually not be ignoring unknown properties, because there are none. The response is exactly what you expect:


{
"FaultId": "User already exists",
"fault": "FAULT_USER_ALREADY_EXISTS"
}



But your response class is misconfigured. Try annotating your fields with @JsonProperty.


public class RegistrationFailureResponse {

@JsonProperty private String FaultId;
@JsonProperty private String fault;
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV