【问题标题】:How to stub multiple parameter Springboot Restful POST Endpoint using WireMock如何使用 WireMock 存根多个参数 Springboot Restful POST Endpoint
【发布时间】:2019-07-02 04:41:35
【问题描述】:

我是wiremock的新手,并试图对以下springboot restful端点的调用进行存根。

@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 Not Found

//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)));

请帮忙提供代码 sn-p 或参考。

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • @olatom 这对你有帮助吗?
    • 您好,Madhu,感谢您的快速回复。但这没有用。我得到一个 org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found 我确实将查询字符串更改为 template=a&templateDataAsJson=a",认为这是因为编码的 html 和 json 参数很大,但不是。我是正确地说查询字符串用于 GET 请求。请问如何发送 POST 请求的参数?这可能是问题所在。
    猜你喜欢
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多