WireMock 有时表现得很奇怪

WireMock behaves weird sometime

在大多数集成测试中,我使用 spring-boot-test(2.1.9.RELEASE) 和 spring-cloud-contract-wiremock(2.0.2.RELEASE).测试是基于 @AutoConfigureWireMock(port = 0) 启动 WireMock 服务器,所以我没有使用任何 WireMockRule 或其他配置设置。

有时验证失败并出现非常奇怪的错误:

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

如您所见,预期端点与实际调用完全相同。

你有什么想法吗?或者你以前见过吗? 这里有一个未解决的问题:https://github.com/tomakehurst/wiremock/issues/706,但回复不是很有帮助。

我在 DELETE 上遇到了同样的问题,但在本地它工作(windows + intelliJ)并且在 Jenkins(linux)上失败。你呢?

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

编辑:

解决方法: 我的算法中有一个异步方法,我不需要等待它回答完成算法所以我必须放一个 Thread.sleep 以确保调用完成

    /**
     * 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();
            }
    }

因此,在构建随机失败数月之后,似乎唯一的解决方案是等待存根注册。所以新的验证包装器方法看起来像这样:

    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);
}

来电者是这样使用的:

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

现在我们终于可以依靠我们的 IT 构建管道了。祝大家只有绿色建筑:)