单测运行正常,多测并行时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
相关产品推荐
相关产品推荐

