You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单测运行正常,多测并行时coVerify验证失败问题排查

OpenWeatherMap Mockk 多测试场景下coVerify验证失败排查

API调用代码

使用OpenWeatherMap获取历史天气数据的调用代码如下:

openWeatherMapAPI.oneCall().historical()
    .byCoordinateAndTimestamp(
        Coordinate.of(latitude.toDouble(), longitude.toDouble()),
        startDateTime.toEpochSecond()
    )
    .language(Language.ENGLISH)
    .unitSystem(UnitSystem.METRIC)
    .retrieve()
    .asJava()

Mock实现

通过Mockk构造函数Mock的代码:

mockkConstructor(OpenWeatherMapClient::class)
coEvery {
    anyConstructed<OpenWeatherMapClient>().oneCall()
        .historical().byCoordinateAndTimestamp(any(), any())
        .language(Language.ENGLISH)
        .unitSystem(UnitSystem.METRIC)
        .retrieve()
        .asJava()
} returns testUtilsImplementation.getSuccessfulHistoricalWeatherResponse()

调用验证代码

验证API调用次数的代码:

coVerify(exactly = 5) {
    anyConstructed<OpenWeatherMapClient>().oneCall()
        .historical().byCoordinateAndTimestamp(any(), any())
        .language(Language.ENGLISH)
        .unitSystem(UnitSystem.METRIC)
        .retrieve()
        .asJava()
}

问题现象

单独运行该测试时一切正常,但同时运行多个测试时,coVerify验证失败,报错信息如下:

Verification failed: call 1 of 7: OpenWeatherMapClient(mockkConstructor<OpenWeatherMapClient>()).oneCall()) was not called

疑问

  • 预期是调用次数过多导致失败,但报错显示甚至未调用一次;
  • 报错中的数字7来自何处?
  • 添加unmockkConstructor(OpenWeatherMapClient::class)、unMockAll()或clearAllMocks()均无效果,该从哪些方向排查?

内容的提问来源于stack exchange,提问作者Simon Balling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:31:20