2017-05-02 70 views
0

我有一个简单的测试,在测试中,如果json的POST成功。这是我的测试:POST JSON okhttp

int expectedCodeResponse = 201; 
Map<String, String> params = new HashMap<String, String>(); 
    params.put("batchPublicId", "001"); 
    params.put("fileSize", "2"); 
    params.put("location", "{\\\"bucket\\\":\\\"blabla\\\"}"); 
    params.put("title", "testInvoice"); 
    JSONObject parameter = new JSONObject(params); 

OkHttpClient httpClient = new OkHttpClient(); 

    RequestBody body = RequestBody.create(JSON, parameter.toString()); 
    Request request = new Request.Builder() 
      .url(environmentUrls.get("base_url") + "/invoices") 
      .post(body) 
      .addHeader("content-type", "application/json; charset=utf-8") 
      .build(); 


    Response response = httpClient.newCall(request).execute(); 

    Assert.assertEquals(response.code(), expectedCodeResponse); 

现在它不再工作,因为fileSize不是一个字符串了,它是一个int。我该如何解决这个问题?

回答

0

将您的字符串转换为int like this,并评估这两个整数的相等性。

... 

    Response response = httpClient.newCall(request).execute(); 

    Assert.assertEquals(Integer.parseInt(response.code()), expectedCodeResponse);