Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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",  "[email protected]"); 


        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

like image 436
Aakash Goyal Avatar asked Oct 16 '25 18:10

Aakash Goyal


1 Answers

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;
}
like image 53
Luciano van der Veekens Avatar answered Oct 19 '25 08:10

Luciano van der Veekens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!