'Spring Cloud Contract with Wiremock - how to choose Scenario manually?

Im working in a project using Spring Cloud Contract and Wiremock to create integration tests. Everything was fine until we need to add multiple responses to the same request in Wiremock. Reading some docs, i think Scenarios from Wiremock is the way to go.... but since we are using the 'new' @AutoConfigureWiremock annotation, i can't figure out how to work with wiremock scenarios...

Doesnt matter what i do, wiremock always fallback to the 'Started' Scenario everytime. I can't use my 'Expired' Scenario....

My BaseClassCOnfiguration

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureWireMock
@AutoConfigureStubRunner(stubsMode = StubRunnerProperties.StubsMode.LOCAL)
@Import(value = TestConfigClass.class)
@ContextConfiguration(initializers = {Properties.class})
@AutoConfigureMockMvc(addFilters = true)
public abstract class SpringCloudContractBase {

My WireMockClassRule

    @ClassRule
    public static WireMockClassRule wiremock =
        new WireMockClassRule(options()
            .dynamicPort()
            .extensions(new ResponseTemplateTransformer(true)));

My mapping scenarios

  "mappings": [
    {
      "priority": 1,
      "scenarioName": "att",
      "requiredScenarioState": "Started",
      "request": {
        "method": "GET",
        "urlPath": "/v1/authtoken/exchange"
      },
      "response": {
        "headers": {
          "Content-Type": "application/json"
        },
        "status": 200,
        "bodyFileName": "{{request.pathSegments.[1]}}/{{request.pathSegments.[2]}}/index.json"
      }
    },
    {
      "priority": 2,
      "scenarioName": "att",
      "requiredScenarioState": "Expired",
      "request": {
        "method": "GET",
        "urlPath": "/v1/authtoken/exchange"
      },
      "response": {
        "status": 403
      }
    }
  ]
}

And finally my test

@Test
  public void test__stub__return403() {
    // THIS DOES NOT WORK ! Will pick the first scenario under my mappings instead.....
    wiremock.stubFor(get(anyUrl()).inScenario("att").whenScenarioStateIs("Expired"));
    Auth auth = service.exchange("token");
  }

How can i achieve this ? Thank you !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source