MockServer意外响应并显示为空

问题描述

我们来看一个测试,该测试使用MockServer(org.mock-server:mockserver-netty:5.10.0)来模拟响应。

预计响应主体将等于字符串"something"。 但是,该测试失败了,因为响应主体是一个空字符串。

  @Test 
  void test1() throws Exception {

    var server = ClientAndServer.startClientAndServer(9001);

    server
        .when(
            request().withMethod("POST").withPath("/checks/"),exactly(1)
        )
        .respond(
            response()
                .withBody("\"something\"")
                .withStatusCode(205)
                .withHeader("Content-Type","application/json")
        );

    HttpRequest request = HttpRequest.newBuilder()
                                     .uri(URI.create("http://localhost:9001/checks/"))
                                     .POST(BodyPublishers.noBody())
                                     .build();

    HttpResponse<String> response =
        HttpClient.newHttpClient().send(request,BodyHandlers.ofString());

    assertEquals(205,response.statusCode());
    assertEquals("something",response.body()); // fails
  }

如何使响应主体与response().withBody(...)中提供的字符串相等?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)