MockRestServiceServer: how to mock a POST call with a body?

我的梦境 提交于 2020-06-10 03:45:59

问题


I am trying to mock a POST method with MockRestServiceServer in the following way:

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

Problem: How do I verify a request body in this setup?

I browsed through the documentation and some examples and still can't figure out how it can be done.


回答1:


You can use content().string to verify body:

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

Or content().bytes:

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

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




回答2:


How I would do such kind of test. I would expect on the mocked server the proper body to be received in String format and if this body has been received, the server will respond with the proper response body in String format. When I receive the response body, I would map it to the POJO and check all the fields. Also, I would map the request from String to POJO before send. So now we could check that mappings work in both directions and we can send requests and parse responses. Code would be something like this:

  @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(""));
  }



回答3:


May use HttpMessageConverter can help. according to the document, HttpMessageConverter::read method can be the place that provide check the input ability.



来源:https://stackoverflow.com/questions/57328602/mockrestserviceserver-how-to-mock-a-post-call-with-a-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!