我有以下功能(这个功能并不重要):
fun myRandomFunc(something: String?): List<Int> {
return listOf(5)
}
您可以想象它正在执行一些API调用、返回一些对象的列表等等。我可以很容易地在测试中像这样模拟这个函数:
doReturn(
listOf(
5
)
)
.whenever(...).myRandomFunc("something")
但是,在我在混合中引入(重试/恢复)之后,这个模拟现在正在抛出org.mockito.exceptions.misusing.NotAMockException at ...
。知道为什么吗?
这是spring重试的代码:
@Retryable(
value = [ApiException::class], maxAttempts = MAX_RETRIES,
backoff = Backoff(delay = RETRY_DELAY, multiplier = RETRY_MULTIPLIER, random = true)
)
fun myRandomFunc(something: String?): List<Int> {
return listOf(5)
}
@Recover
fun testMyRandomFunc(exception: Exception): List<Int> {
log.error("Exception occurred ...", exception)
throw RemoteServiceNotAvailableException("Call failed after $MAX_RETRIES retries")
}
代码工作,它是功能性的,只是对测试的模拟现在被打破了。会很感激你的帮助
发布于 2021-03-24 14:57:21
Spring重试在对象周围创建一个代理。
如果有接口,则代理是JDK代理;如果没有,则使用CGLIB。
Mockito不能模拟CGLIB (最终)方法。
https://stackoverflow.com/questions/66783342
复制相似问题