如何在WireMock的ResponseBody中传递json响应

时间:2018-05-03 12:16:44

标签: wiremock

我正在尝试验证json响应。已经完成了wiremock中的文档,并尝试了github示例,如下面的语法。

https://github.com/tomakehurst/wiremock/blob/master/src/test/java/ignored/Examples.java#374

@Test
public void toDoListScenario() {
stubFor(get(urlEqualTo("/todo/items")).inScenario("To do list")
            .whenScenarioStateIs(STARTED)
            .willReturn(aResponse()
                    .withBody("<items>" +
                            "   <item>Buy milk</item>" +
                            "</items>"))); }

如何在Body Section中输入json响应。

 {
 employeeDetails : [
   employeeName : ABCDE,
   employeeID : 12345 ]
 }

以下表示是否正确。请帮帮我。

.withBody("employeeDetails:" +
          "employeeID" : "12345" +
          "employeeName" : "Preethi" + )

1 个答案:

答案 0 :(得分:1)

将文本格式化为json:

.withBody("{\"employeeDetails\":[" +
      "\"employeeID\" : \"12345\"" +
      "\"employeeName\" : \"Preethi\"]}")

并将标题设置为

.withHeader("Content-Type", equalTo("application/json"))