WireMock behaves weird sometime
Asked Answered
C

2

9

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.

Continuate answered 16/1, 2020 at 11:12 Comment(2)
Is there a particular reason you're using WireMock version 2.0.2? The current release is 2.25.1. See the WireMock documentation for specific details.Woken
It's spring-cloud-contract-wiremock 2.0.2 version(that include WireMock 2.18.0 version)Continuate
C
5

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 :)

Continuate answered 12/10, 2020 at 15:0 Comment(1)
Thank you for coming back after almost 9 months to post this answer ❤Semipalatinsk
S
3

I have the same issue on the DELETE, but on local it's working (windows + intelliJ) and on Jenkins (linux) fail. And you ?

com.github.tomakehurst.wiremock.client.VerificationException: 
No requests exactly matched. Most similar request was:  expected:<
DELETE
/myAPI/api
> but was:<
DELETE
/myAPI/api
>

Edit:

Solution: I have an asynchronous method in my algorithm and I don't need to wait for it to answer to finish the algo so I have to put a Thread.sleep to be sure that the call is done

    /**
     * Use It when you have a asyc call in your methode
     * @param delay time to wait
     */
    public void waitingForAsycRequest(int delay) {
            try {
                Thread.sleep(delay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
Supination answered 25/3, 2020 at 8:39 Comment(3)
Most of the times is failing in CI env. We're using Bamboo instead of Jenkins. But surprisingly rarely is failing from the IntelliJ+Ubuntu too. And more often from de cmd when I'm running the whole test pack with Gradle. Still we haven't resolved the issue.. And it's pretty frustrating !!!Continuate
Is this the solution to the origianal question or your own problem?Windburn
It is the solution to my problem, which resembles its problem because I have the same instability when I run all my unit tests.@brebDev Have you tested the anotation @RepeatedTest (value = 100) to see if it goes from time to time? it helped me a lotSupination

© 2022 - 2024 — McMap. All rights reserved.