-1

I have to create a mock server expectation to check whether incoming request equal to mocked request. My problem is Json which is in the expectation same as which i provided but i`m getting

java.lang.AssertionError: 
Expected :401
Actual   :404

Json for expectation -

String rechargeRequest = "{\n" +
            "   \"RechargeRequest\":{\n" +
            "      \"RechargeSerialNo\":\"2645\",\n" +
            "      \"RechargeChannelID\":\"3\",\n" +
            "      \"RechargeObj\":{\n" +
            "         \"SubAccessCode\":{\n" +
            "            \"PrimaryIdentity\":\"763500001\"\n" +
            "         }\n" +
            "      },\n" +
            "      \"RechargeInfo\":{\n" +
            "         \"CashPayment\":[\n" +
            "            {\n" +
            "               \"Amount\":\"30\"\n" +
            "            }\n" +
            "         ]\n" +
            "      }\n" +
            "   }\n" +
            "}\n";

My expectation is -

 private  void createdExpectation( ){
   new MockServerClient("127.0.0.1", 1081)
           .when(
                   request()
                           .withMethod("POST")
                           .withPath("/cbs/billingmgt/v1.0/Account/Recharge")
                           .withHeader("\"Content-type\", \"application/json\"")
                           .withBody(rechargeRequest))
           .respond(
                   response()
                           .withStatusCode(401)
                           .withBody("Done")
           );
}

My HtttpResponse class to create request -

private org.apache.http.HttpResponse hitTheServerWithPostRequest(String jsonString) {
    String url = "http://127.0.0.1:1081/cbs/billingmgt/v1.0/Account/Recharge";
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);
    post.setHeader("Content-type", "application/json");
    org.apache.http.HttpResponse response=null;

    try {
        StringEntity stringEntity = new StringEntity(jsonString);
        post.getRequestLine();
        post.setEntity(stringEntity);
        response=client.execute(post);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return response;
}

My test method - where i creating sample Json to pass to the expectation

@Test
public  void test() throws JsonProcessingException {
    JsonRechargeRequest jsonRechargeRequest = new JsonRechargeRequest();
    RechargeRequest request = new RechargeRequest();
    request.setRechargeSerialNo("2645");
    request.setRechargeChannelID("3");
    RechargeObj rechargeObj = new RechargeObj();
    SubAccessCode subAccessCode = new SubAccessCode();
    subAccessCode.setPrimaryIdentity("763500001");
    rechargeObj.setSubAccessCode(subAccessCode);
    RechargeInfo rechargeInfo = new RechargeInfo();
    CashPayment cashPayment = new CashPayment();
    cashPayment.setAmount("30");
    rechargeInfo.setCashPayments(Collections.singletonList(cashPayment));
    request.setRechargeObj(rechargeObj);
    request.setRechargeInfo(rechargeInfo);
    jsonRechargeRequest.setRechargeRequest(request);

    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(jsonRechargeRequest);

    //Calling Expectation
    createdExpectation();
    org.apache.http.HttpResponse response = hitTheServerWithPostRequest(jsonString);
    assertEquals(401, response.getStatusLine().getStatusCode());
}

In this Test method assertEquals Expect 401 but response from the hitTheServerWithPostRequest methods response status 404. how it happens?

Thanks, Dasun.

Das_J
  • 71
  • 2
  • 9

1 Answers1

0

In the light of lack of any answers...here is a guess:

In the createdExpectation( ) method, instead of

.withHeader("\"Content-type\", \"application/json\"")

it has to be

.withHeader("Content-type", "application/json")

or

.withHeader(header("Content-type", "application/json"))
gears
  • 690
  • 3
  • 6
  • Bro I found the answer .Here passed two Json values are same but the format kinda different. So to be assert equal true have to make two jsons into json objects. – Das_J Dec 12 '19 at 03:32
  • I ll post the answer here later. – Das_J Dec 12 '19 at 03:33