简体   繁体   English

为什么我从 Java 中的 Mock Server 得到 Assert Not Equal?

[英]Why am i getting Assert Not Equal from the Mock Server in Java?

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我的问题是 Json,它与我提供的预期相同,但我得到了

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

Json for expectation -期望的 Json -

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 -我的 HttpResponse 类创建请求 -

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我的测试方法 - 我创建示例 Json 以传递给期望

@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?在这个测试方法中,assertEquals 预期为 401,但来自 hitTheServerWithPostRequest 方法的响应状态为 404。它是如何发生的?

Thanks, Dasun.谢谢,大孙。

In the light of lack of any answers...here is a guess:鉴于缺乏任何答案......这里是一个猜测:

In the createdExpectation( ) method, instead ofcreatedExpectation( )方法中,而不是

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

it has to be它一定要是

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

or或者

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM