javaunit-testingjunitjersey-test-framework

UnrecognizedPropertyException in Jersey Test Framework


I am new to Jersey test framework. I am trying to implement test cases using jersey test framework in my project. I have a REST service whose url will be :

http://localhost:8080/EzSupportBackend/a/dealerservices/getdealerdetails/FAB/ACZOKuBmtvyni16eMJ3AoSVg_HxL3bh3Lz0WiWNJhXudh9M90LSc8bDHD-Y2JpcbISZcC_DM2PL4yqSsmXUKA65ZmvuoiQ_wotgU1OvA8GGw_yPMwVnXGg==

I tried to test this service using Jersey with the below code:

public class DealerServicesTest extends JerseyTest{
@Override
protected AppDescriptor  configure() {
    return new WebAppDescriptor.Builder().build();
}

@Test
public void testGetDealerDetails() throws JSONException,URISyntaxException {
    WebResource webResource = client().resource("http://localhost:8080/");
    JSONObject json = webResource.path("EzSupportBackend/a/dealerservices/getdealerdetails/FAB/ACZOKuBmtvyni16eMJ3AoSVg_HxL3bh3Lz0WiWNJhXudh9M90LSc8bDHD-Y2JpcbISZcC_DM2PL4yqSsmXUKA65ZmvuoiQ_wotgU1OvA8GGw_yPMwVnXGg==").get(JSONObject.class);

    assertEquals("ONLINE", json.get("companyType"));
    assertEquals("FabFurnish", json.get("companyName"));        
    assertEquals("Gurgoan,Haryana, India", json.get("companyAddress"));
    assertEquals("04222456803", json.get("phoneNumber"));
    assertEquals("ACTIVE", json.get("status"));     
    assertEquals("customerservice@fabfurnish.com", json.get("emailId"));
}
}

When i test the service thro Postman (Chrome Extension) , i get a proper response as below:

{
  "companyType": "ONLINE",
  "companyName": "FabFurnish",
  "companyAddress": "Gurgoan,Haryana, India",
  "companyLocation": null,
  "longitude": "77.023419",
  "phoneNumber": "04222456803",
  "serviceRating": 0,
  "repairRating": 0,
  "warrantyRating": 0,
  "shopRating": 0,
  "status": "ACTIVE",
  "createdOn": 1446613095557,
  "updatedOn": 1446613095557,
  "companyId": "FAB",
  "lattitde": "28.47427",
  "emailId": "customerservice@fabfurnish.com",
  "createdby": null,
  "updatedby": null
}

But thro this test i get the below exception:

com.sun.jersey.api.client.ClientHandlerException:     com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "companyType" (class org.json.JSONObject), not marked as ignorable (0 known properties: ])
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@45b4c3a9; line: 1, column: 17] (through reference chain: org.json.JSONObject["companyType"])
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:563)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:674)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
    at com.vs.mhs.ezsupport.services.DealerServicesTest.testGetDealerDetails(DealerServicesTest.java:27)

Anyone can help me figure out why??

EDIT: My code looks like below:

@PermitAll
@GET
@Path("/getdealerdetails/{companyId}/{token_id}")
public Response getDealerDetails(@PathParam("companyId") String companyId, @Context SecurityContext userContext, @Context HttpServletRequest request){
    boolean isUserAuthorised = isUserAuthenticated(userContext);
    DealerDetails dealer = null;
    DealerDetailsView getDealerView = null;
    if(isUserAuthorised){
        EntityManager em = (EntityManager) request.getAttribute(FilterConstants.ENTITYMANAGER);
        DealerDetailsBDL dealerbdl = new DealerDetailsBDL(em);
        dealer = dealerbdl.getDealerDetails(companyId);
        getDealerView = new DealerDetailsView(dealer);
    }
    return Response.ok(getDealerView).build();          
}

DealerDetailsView is a class with private variables for the properties i ve listed below and getters and setters:

private String companyid;
private String companyType;
private String companyName;
private String companyAddress;
private String companyLocation;
private String lattitude;
private String longitude;
private String phoneNumber;
private String emailid;
private int serviceRating;
private int repairRating;
private int warrantyRating;
private int shopRating;
private String status;

Solution

  • Jackson is the JSON provider, and Jackson generally works with model POJOs, which JSONObject is not. Jackson is looking for a companyType property in JSONObject, which it doesn't have. That's why you are getting the exception. If you don't have a POJO specifically for that JSON, then just get it as a String, and create the JSONObject with that string

    String jsonString = webResource...get(String.class);
    JSONObject json = new JSONObject(jsonString);
    

    UPDATE

    A POJO for Jackson is just a class that maps the JSON fields to class properties (that follow JavaBean namming convention). For example for these two JSON fields

    "companyType": "ONLINE",
    "companyName": "FabFurnish"
    

    You can have a class like

    public class CompanyInfo {
        private String companyType;
        private String companyName;
    
        public CompanyInfo() {}
    
        public String getCompanyInfo() { return companyInfo; }
        public void setCompanyInfo(String info) { this.companyInfo = info; }
        
        public String getCompanyType() { return companyType; }
        public void setCompanyType(String type) { this.companyType = type; }
    }
    

    To match the JSON field with the Java property, the property (getter/setter) should be prefixed with get/set and the exact name of the field, with the first letter capitalized, as shown above. So just complete the POJO with all the other JSON fields, and the serialization should work with CompanyInfo.