如何使用WireMock存根多个参数Springboot Restful POST端点

时间:2019-07-02 04:41:35

标签: java spring-boot spring-mvc mockito wiremock

我是Wiremock的新手,并试图阻止以下springboot静态端点的调用。

@PostMapping(path = "/template/pdf", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<String> bindData(
        @ApiParam(value = "BindDataRequest payload", required = true)
        @RequestParam String template, @RequestParam String templateDataAsJson) throws IOException {

   //Some code 
    return ResponseEntity.ok("xyz");
}

**The following basic logic works:**

templatingService.stubFor(
                post(urlEqualTo("/template/pdf"))
                        .willReturn(aResponse().withBody(JSON_INPUT_TO_PDF_GEN).withStatus(200)));

但是,在调用.willReturn(.....)

之前,我需要一种设置2个字符串请求参数的方法。

我尝试过:

templateBinderService.stubFor(
                post(urlEqualTo("/template/pdf"))
                        .withRequestBody(WireMock.equalTo("jixhcjxhcjxhcxhchx"))
                        .withRequestBody(WireMock.equalTo("nhhhxhxhhhhhxhhhh"))
                        .willReturn(aResponse().withBody(JSON_INPUT_TO_HTML2PDF_GEN).withStatus(200)));

但是得到了:

org.springframework.web.client.HttpClientErrorException $ NotFound:404未找到

//I have also tried:

templateBinderService.stubFor(
                post(urlEqualTo("/template/test"))
.withRequestBody(containing("param1-value"))
.withRequestBody(containing("param2-value"))

                        .willReturn(aResponse().withBody("i-am-a-response").withStatus(200)));

//I have also tried:

    templateBinderService.stubFor(
                    post(urlEqualTo("/template/test"))
                            .withRequestBody(equalToJson("{}"))
                            .willReturn(aResponse().withBody("i-am-a-response").withStatus(200)));

请帮助提供代码段或参考。

1 个答案:

答案 0 :(得分:0)

由于参数templatetemplateDataAsJson都用@RequestParam进行了注释,因此,它们应相应地在Wiremock存根中传递,如下所示。

templatingService.stubFor(
        post(urlEqualTo("/template/pdf?template=value1&templateDataAsJson=value2"))
       .willReturn(aResponse().withBody(JSON_INPUT_TO_PDF_GEN).withStatus(200)));

其中value1value2是两个参数各自的值。