4

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

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
Aakash Goyal
  • 305
  • 1
  • 4
  • 22
  • Possible duplicate of [Jackson with JSON: Unrecognized field, not marked as ignorable](https://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable) – vahdet Jun 26 '18 at 18:41

1 Answers1

2

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;
}
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • 1
    @AakashGoyal I believe by default Jackson searches for getters/setters to find properties in a class. Because your class doesn't have those, you need the annotations. – Luciano van der Veekens Jul 03 '18 at 09:30