【问题标题】:Wiremock: Multiple responses for the same URL and content?Wiremock:相同 URL 和内容的多个响应?
【发布时间】:2017-07-28 09:56:23
【问题描述】:

也在此处分享:https://github.com/tomakehurst/wiremock/issues/625

我正在编写一个集成测试来验证我与 REST API 交互的应用程序是否能正确处理不成功的请求。为此,我想模拟一个向 HTTP 端点发出两次 GET 请求的场景。第一次请求不成功,响应状态码为500;第二次,请求成功,响应状态码为 200。

考虑下面的例子:

@Rule
public WireMockRule wireMockRule 
        = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());

@Test
public void testRetryScenario(){

    // First StubMapping
    stubFor(get(urlEqualTo("/my/resource"))
            .withHeader("Accept", equalTo("text/xml"))
            .willReturn(aResponse()
                .withStatus(500) // request unsuccessful with status code 500
                .withHeader("Content-Type", "text/xml")
                .withBody("<response>Some content</response>")));

    // Second StubMapping
    stubFor(get(urlEqualTo("/my/resource"))
            .withHeader("Accept", equalTo("text/xml"))
            .willReturn(aResponse()
                .withStatus(200)  // request successful with status code 200
                .withHeader("Content-Type", "text/xml")
                .withBody("<response>Some content</response>")));

    //Method under test that makes calls to endpoint
    doSomething();

    Thread.sleep(5000);

    //Verify GET request was made again after first attempt
    verify(exactly(2), getRequestedFor(urlEqualTo("/my/resource")));

}

有没有办法避免第二个 StubMapping 覆盖第一个 - 以确保 doSomething() 第一次发出请求时,状态码为 500 的响应 返回,第二次返回状态码为 200 的不同响应

【问题讨论】:

    标签: unit-testing testing integration-testing wiremock


    【解决方案1】:

    这就是场景功能的用途。

    您需要将两个存根放入一个场景(即相同的场景名称),使第一个存根触发转换到新状态,然后使第二个存根取决于场景处于第二个状态和第一个存根存根取决于处于STARTED 状态的场景。

    见:http://wiremock.org/docs/stateful-behaviour/

    【讨论】:

      【解决方案2】:

      使用“场景”功能,这样的事情有所帮助:

      // First StubMapping
      stubFor(get(urlEqualTo("/my/resource"))
              .withHeader("Accept", equalTo("text/xml"))
              .inScenario("Retry Scenario")
              .whenScenarioStateIs(STARTED)
              .willReturn(aResponse()
                  .withStatus(500) // request unsuccessful with status code 500
                  .withHeader("Content-Type", "text/xml")
                  .withBody("<response>Some content</response>"))
              .willSetStateTo("Cause Success")));
      
      // Second StubMapping
      stubFor(get(urlEqualTo("/my/resource"))
              .withHeader("Accept", equalTo("text/xml"))
              .inScenario("Retry Scenario")
              .whenScenarioStateIs("Cause Success")
              .willReturn(aResponse()
                  .withStatus(200)  // request successful with status code 200
                  .withHeader("Content-Type", "text/xml")
                  .withBody("<response>Some content</response>")));
      

      【讨论】:

        猜你喜欢
        • 2014-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多