Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey测试框架中存在无法识别的属性异常_Java_Unit Testing_Junit_Jersey Test Framework - Fatal编程技术网

Java Jersey测试框架中存在无法识别的属性异常

Java Jersey测试框架中存在无法识别的属性异常,java,unit-testing,junit,jersey-test-framework,Java,Unit Testing,Junit,Jersey Test Framework,我是新泽西州的测试框架。我试图在我的项目中使用jersey测试框架实现测试用例。我有一个REST服务,其url为: http://localhost:8080/EzSupportBackend/a/dealerservices/getdealerdetails/FAB/ACZOKuBmtvyni16eMJ3AoSVg_HxL3bh3Lz0WiWNJhXudh9M90LSc8bDHD-Y2JpcbISZcC_DM2PL4yqSsmXUKA65ZmvuoiQ_wotgU1OvA8GGw_yPMwVn

我是新泽西州的测试框架。我试图在我的项目中使用jersey测试框架实现测试用例。我有一个REST服务,其url为:

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

我尝试使用Jersey测试此服务,代码如下:

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"));
}
}
当我通过邮递员(Chrome扩展)测试服务时,我得到如下正确响应:

{
  "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
}
但通过这个测试,我得到了以下例外:

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)
任何人都可以帮我找出原因

编辑: 我的代码如下所示:

@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是一个具有私有变量的类,用于我在下面列出的属性以及getter和setter:

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;

Jackson是JSON提供程序,Jackson通常与POJO模型一起工作,后者不是。Jackson正在
JSONObject
中寻找一个
companyType
属性,但它没有。这就是为什么你会得到例外。如果没有专门针对该JSON的POJO,那么只需将其作为字符串获取,并使用该字符串创建
JSONObject

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

更新 Jackson的POJO只是一个将JSON字段映射到类属性(遵循JavaBean命名约定)的类。例如,对于这两个JSON字段

"companyType": "ONLINE",
"companyName": "FabFurnish"
你可以上这样的课

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; }
}

要将JSON字段与Java属性匹配,属性(getter/setter)的前缀应为
get/set
,字段的确切名称,首字母大写,如上所示。因此,只需使用所有其他JSON字段完成POJO,序列化就可以与
CompanyInfo

一起工作。您的POJO中似乎不知道
companyType
。Patrick,我已经编辑了这个问题来显示映射类DealerDetailsView。这就是你想要的吗?这成功了,我得到了一个字符串。谢谢你@peeskillet。您能告诉我如何返回该JSON的pojo吗。我的代码如下所示:谢谢@peeskillet。在本例中,我将发送一个名为DealerDetailsView的POJO。有关代码,请参见我问题中的编辑。那么,你不认为我的回答是POJO和String吗?我不明白你的问题。响应以原始JSON形式出现,可以作为原始字符串检索。如果您想将其作为POJO获取,那么使用
get(CompanyInfo.class)
get(DealerDetailsView.class)
。。。非常感谢:-)我还有一个疑问。如果我返回CompanyInfo类的数组呢?你是如何在我的测试课上得到它的?