【问题标题】:WireMock behaves weird sometimeWireMock 有时表现得很奇怪
【发布时间】:2020-05-03 05:22:04
【问题描述】:

在大多数集成测试中,我使用的是 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:没有完全匹配的请求。最相似的请求是:预期:

但是:

正如您在上面看到的,预期的端点与实际调用完全相同。

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

【问题讨论】:

  • 您使用 WireMock 版本2.0.2 有什么特殊原因吗?当前版本是2.25.1。具体细节请见WireMock documentation
  • 是spring-cloud-contract-wiremock 2.0.2版本(包括WireMock 2.18.0版本)

标签: java spring-boot-test wiremock


【解决方案1】:

我在 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();
            }
    }

【讨论】:

  • 大多数时候在 CI 环境中都失败了。我们使用 Bamboo 而不是 Jenkins。但令人惊讶的是,IntelliJ+Ubuntu 也很少失败。当我使用 Gradle 运行整个测试包时,通常来自 de cmd。我们仍然没有解决这个问题.. 这很令人沮丧!!!
  • 这是原始问题的解决方案还是您自己的问题?
  • 这是我的问题的解决方案,这类似于它的问题,因为我在运行所有单元测试时都有同样的不稳定性。@brebDev 你有没有测试过注解@RepeatedTest (value = 100) 看它是否来自时不时?它对我帮助很大
【解决方案2】:

因此,经过数月随机失败的构建,似乎唯一的解决方案是等到存根注册。所以新的验证包装器方法如下所示:

    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 构建管道了。祝大家只有绿色版本:)

【讨论】:

    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多