MockRestServiceServer:如何模拟带有请求体的POST调用?

25

我正尝试使用MockRestServiceServer模拟一个POST方法,具体如下:

MockRestServiceServer server = bindTo(restTemplate).build();
server.expect(requestTo("/my-api"))
        .andExpect(method(POST))
        .andRespond(withSuccess(expectedResponce, APPLICATION_JSON));

问题:如何在这种设置中验证请求正文?

我浏览了文档和一些示例,仍然无法弄清楚如何完成。


你为什么想要检查请求体?这是输入数据,不应该被验证。你是不是打错了,想要检查响应体? - Igor Khvostenkov
@IgorKhvostenkov 这是一个POST请求,意味着它会发送一段数据。我想要验证发送的信息是否正确。 - Sasha Shpota
2
如果你想验证请求正文,我认为你的方法不正确。你只需要测试如何创建正文,而不是模拟 API 并在那里测试发送的内容。 - stacker
无论是GET还是POST,这都不重要。从概念上讲,测试某些你手动定义为正确或错误的东西是很奇怪的,但它并不是由生产代码决定的。你可以采用@user7294900提出的方法,但更多地是缩小模拟范围或使用更精确的触发器,而不是验证你的生产代码。 - Igor Khvostenkov
@IgorKhvostenkov,也许“验证”不是这里最好的词。让我解释一下。我正在编写一个集成测试。我不想从测试中调用真实的API,而是模拟API。但是,如果我不检查请求体,模拟的API将为所有请求返回成功响应,这是我不想要的。 - Sasha Shpota
好的,我明白你的想法了,已发布示例说明我如何做到这一点。 - Igor Khvostenkov
3个回答

20

你可以使用 content().string 来验证请求体:

.andExpect(content().string(expectedContent))

或者使用 content().bytes

this.mockServer.expect(content().bytes("foo".getBytes()))

this.mockServer.expect(content().string("foo"))


0

可以使用HttpMessageConverter来帮助。 根据文档,HttpMessageConverter::read方法可以提供检查输入能力的地方。


0

我会如何进行这种类型的测试。我期望在模拟服务器上接收到正确的请求体,且以String格式接收到该请求体,如果确实接收到了该请求体,服务器将以String格式响应正确的响应体。当我收到响应体时,我会将其映射到POJO并检查所有字段。此外,在发送之前,我还会将请求从String映射为POJO。所以现在我们可以检查映射是否双向工作,并且可以发送请求和解析响应。代码大致如下:

  @Test
  public void test() throws Exception{
    RestTemplate restTemplate = new RestTemplate();
    URL testRequestFileUrl = this.getClass().getClassLoader().getResource("post_request.json");
    URL testResponseFileUrl = this.getClass().getClassLoader().getResource("post_response.json");
    byte[] requestJson = Files.readAllBytes(Paths.get(Objects.requireNonNull(testRequestFileUrl).toURI()));
    byte[] responseJson = Files.readAllBytes(Paths.get(Objects.requireNonNull(testResponseFileUrl).toURI()));
    MockRestServiceServer server = bindTo(restTemplate).build();
    server.expect(requestTo("http://localhost/my-api"))
          .andExpect(method(POST))
          .andExpect(content().json(new String(requestJson, "UTF-8")))
          .andRespond(withSuccess(responseJson, APPLICATION_JSON));

    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl("http://localhost/my-api");

    ObjectMapper objectMapper = new ObjectMapper();
    EntityOfTheRequest body = objectMapper.readValue(requestJson, EntityOfTheRequest.class);

    RequestEntity.BodyBuilder bodyBuilder = RequestEntity.method(HttpMethod.POST, uriBuilder.build().toUri());
    bodyBuilder.accept(MediaType.APPLICATION_JSON);
    bodyBuilder.contentType(MediaType.APPLICATION_JSON);
    RequestEntity<EntityOfTheRequest> requestEntity = bodyBuilder.body(body);

    ResponseEntity<EntityOfTheResponse> responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<EntityOfTheResponse>() {});
    assertThat(responseEntity.getBody().getProperty1(), is(""));
    assertThat(responseEntity.getBody().getProperty2(), is(""));
    assertThat(responseEntity.getBody().getProperty3(), is(""));
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接