Search code examples
spring-bootjunitspring-testwiremockweb-api-testing

How to make a request file to be executed before another in WireMock?


I have 2 json files for sending request:

First:

  "request": {
    "method": "GET",
    "urlPathPattern": "my/url/([url0-9/-]{13})",
    "queryParameters": {
      "type": {
        "equalTo": "xxx"
      }
    }
  }

Second:

  "request": {
    "method": "GET",
    "urlPathPattern": "/my/url/ID12345678",
    "queryParameters": {
      "type": {
        "equalTo": "xxx"
      }
    }
  }

My case is the second file but the first file with regex is always executed before, so I can not have the expected response for my case. How can I make the second to be executed before the first so that when I send a certain ID it will have the response that I want?


Solution

  • Depending on your specific use case, you could either use Scenarios or Priority. Making a few assumptions, I believe Priority will accomplish this with ease.

    First

    {
      "priority": 5,
      "request": {
        "method": "GET",
        "urlPathPattern": "my/url/([url0-9/-]{13})",
        "queryParameters": {
          "type": {
            "equalTo": "xxx"
          }
        }
      }
    }
    

    Second

    {
      "priority": 1,
      "request": {
        "method": "GET",
        "urlPathPattern": "/my/url/ID12345678",
        "queryParameters": {
          "type": {
            "equalTo": "xxx"
          }
        }
      }
    }
    

    Priority tells WireMock to check certain stubs/mappings before others. So, in this case, WireMock will check the second stub before the first, because it has a higher priority (lower number === higher priority). You could also omit adding a priority to the first mapping, as the default priority is 5.