Wiremock: Multiple responses for the same URL and Content?

Created on 7 Mar 2017  路  3Comments  路  Source: tomakehurst/wiremock

Also shared here: http://stackoverflow.com/questions/42661054/wiremock-multiple-responses-for-the-same-url-and-content

I'm writing an integration test to verify that my application that interacts with a REST API handles unsuccessful requests appropriately. To do this, I'm wanting to simulate a scenario where a GET requests is made twice to a HTTP endpoint. First time, request is not successful with a response status code of 500; second time, request is successful with a response status code of 200. Consider the example below:

@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")));

}

Is there a way to avoid the 2nd StubMapping from overriding the first -- to make ensure that the first time doSomethings() makes a request, _a response with status code 500_ is returned, and the second time, _a different response with status code 200_ is returned?

Most helpful comment

SOLUTION: using wiremock's stateful behaviour http://wiremock.org/docs/stateful-behaviour/

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

All 3 comments

SOLUTION: using wiremock's stateful behaviour http://wiremock.org/docs/stateful-behaviour/

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

Hi, what is the package where STARTED belongs to ?

I dont see that wiremock documentations as well.

Hi, what is the package where STARTED belongs to ?

I dont see that wiremock documentations as well.

import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;

Was this page helpful?
0 / 5 - 0 ratings