Search code examples
spring-bootjava-8integration-testingjunit4wiremock

Wire mock external module api for Integration test


In our project we have an other project B dependency. I'm trying to write Integration tests for this flow.

Project B has hard coded API : https://store-platform-dev/items with some headers.

I tried using wiremock stub with proxiedFrom(https://store-platform-dev/items)

withHost(https://store-platform-dev/items)

And also proxyvia(), nothing seemed to work. Stubbing is not happening and the api call is not returning stubbed response.

How to approach this? Since it's the external project we can't even make changes to their api.


Solution

  • If Project B has the hostname hardcoded to store-platform-dev you only have two options:

    1. Somehow change how store-platform-dev is resolved to get it resolving to WireMock
    2. Run WireMock as a forward proxy (or browser proxy) and configure WireMock as the proxy server on the system running Project B.

    For 1 - if you're running locally obviously you could use /etc/hosts to map store-platform-dev to 127.0.0.1. Or you could use dnsmasq or similar to run your own DNS server to map store-platform-dev to 127.0.0.1.

    For 2 - see "Running as a browser proxy" in WireMock | Proxying. You can run WireMock as a browser proxy programatically as so:

    WireMockServer wireMockServer = new WireMockServer(options()
      .enableBrowserProxying(true)
    );
    wireMockServer.start();