Search code examples
javaspring-boot-testwiremock

WireMock behaves weird sometime


In most of the integration tests I'm using spring-boot-test(2.1.9.RELEASE) and spring-cloud-contract-wiremock(2.0.2.RELEASE). The test is starting up WireMock server based on : @AutoConfigureWireMock(port = 0), so I'm not using any WireMockRule or other configuration set-up.

Sometime the verifying is failing with a really weird error:

com.github.tomakehurst.wiremock.client.VerificationException:` com.github.tomakehurst.wiremock.client.VerificationException: com.github.tomakehurst.wiremock.client.VerificationException: No requests exactly matched. Most similar request was: expected:< POST /api/id/delete

but was:< POST /api/id/delete

As you can see above the expected endpoint is exactly the same with the actual invocation.

Do you have any ideas ? or Have you seen that before ? There is an open issue here: https://github.com/tomakehurst/wiremock/issues/706 , but the responses are not very helpful.


Solution

  • So after months of randomly failing builds, it seems that the only solution was to wait until the stubs got registered. So the new verify wrapper method looks like this:

        public static void verify(int count, RequestPatternBuilder requestPatternBuilder) {
        int maxRetries = 5;
        while (count != WireMock.findAll(requestPatternBuilder)
                .size() && maxRetries > 0) {
            Thread.sleep(1000);
            maxRetries--;
        }
        WireMock.verify(count, requestPatternBuilder);
    }
    

    And the callers used it like this:

            WireMockHelper.verify(1, putRequestedFor(urlMatching((URL.BASE_URL.ACTION).replace("%s", ".*"))));
    

    Finally now we can rely on our IT build pipeline. Wish you all only green builds :)