Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MockRestServiceServer:如何用主体模拟POST调用?_Java_Spring_Spring Boot_Junit_Spring Rest - Fatal编程技术网

Java MockRestServiceServer:如何用主体模拟POST调用?

Java MockRestServiceServer:如何用主体模拟POST调用?,java,spring,spring-boot,junit,spring-rest,Java,Spring,Spring Boot,Junit,Spring Rest,我试图用MockRestServiceServer以以下方式模拟POST方法: MockRestServiceServer server = bindTo(restTemplate).build(); server.expect(requestTo("/my-api")) .andExpect(method(POST)) .andRespond(withSuccess(expectedResponce, APPLICATION_JSON)); 问题:如何在此设置

我试图用
MockRestServiceServer
以以下方式模拟POST方法:

MockRestServiceServer server = bindTo(restTemplate).build();
server.expect(requestTo("/my-api"))
        .andExpect(method(POST))
        .andRespond(withSuccess(expectedResponce, APPLICATION_JSON));
问题:如何在此设置中验证请求正文

我浏览了一些示例,但仍然不知道该如何操作。

您可以使用来验证正文:

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

或:

expect(content().bytes(“foo.getBytes()))

this.mockServer.expect(content().string(“foo”))


我将如何做这样的测试。我希望在模拟服务器上以
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(""));
  }
@测试
public void test()引发异常{
RestTemplate RestTemplate=新RestTemplate();
URL testRequestFileUrl=this.getClass().getClassLoader().getResource(“post_request.json”);
URL testResponseFileUrl=this.getClass().getClassLoader().getResource(“post_response.json”);
byte[]requestJson=Files.readAllBytes(path.get(Objects.requirennull(testRequestFileUrl.tori());
byte[]responseJson=Files.readAllBytes(path.get(Objects.requirennull(testresponsefluerl.toURI());
MockRestServiceServer=bindTo(restTemplate).build();
expect(requestTo)(“http://localhost/my-api"))
.andExpect(方法(后))
.andExpect(content().json(新字符串(requestJson,“UTF-8”))
.andRespond(带有成功(responseJson,APPLICATION_JSON));
UriComponentsBuilder uriBuilder=UriComponentsBuilder.fromHttpUrl(“http://localhost/my-api");
ObjectMapper ObjectMapper=新的ObjectMapper();
EntityOfRequestBody=objectMapper.readValue(requestJson,EntityOfRequest.class);
RequestEntity.BodyBuilder BodyBuilder=RequestEntity.method(HttpMethod.POST,uriBuilder.build().tori());
accept(MediaType.APPLICATION_JSON);
contentType(MediaType.APPLICATION_JSON);
RequestEntity RequestEntity=bodyBuilder.body(body);
ResponseEntity ResponseEntity=restTemplate.exchange(requestEntity,新的参数化TypeReference(){});
资产(responseEntity.getBody().getProperty1()为(“”);
资产(responseEntity.getBody().getProperty2()为(“”);
资产(responseEntity.getBody().getProperty3()为(“”);
}

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

为什么要检查请求正文?这是输入数据,不应进行验证。这仅仅是打字错误,你指的是回复体吗?@IgorKhvostenkov这是一个POST请求,意味着它发送一段数据。我想验证发送的信息是否正确。我认为你做得不对,如果你试图验证请求主体,你只需要测试你如何创建主体,而不是模仿Api并测试你在那里发送的内容……这真的与GET或POST无关。从概念上讲,测试手动定义为正确或错误但不由生产代码决定的东西是很奇怪的。您可以使用@user7294900建议的方法,但更多的是缩小模拟的范围或使用更精确的触发器,而不是验证生产代码。@IgorKhvostenkov“verify”可能不是这里最好的词。让我详细说明一下。我正在写一个集成测试。我不想从测试中得到一个真正的API,而是在模拟API。但是如果我不检查请求主体,模拟API将为所有请求返回一个成功响应,这是我不希望看到的。