我有以下功能(功能并不重要):
fun myRandomFunc(something: String?): List<Int> {
return listOf(5)
}
你可以想象它正在做一些 API 调用,返回一些对象的列表等。我可以很容易地在测试中模拟这个函数,如下所示:
doReturn(
listOf(
5
)
)
.whenever(...).myRandomFunc("something")
但是在我在混合中引入(重试/恢复)之后,该模拟现在正在抛出
org.mockito.exceptions.misusing.NotAMockException at ...
. 知道为什么吗?
这是弹簧重试的代码:
@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")
}
代码有效,功能强大,只是测试的模拟现在被打破了。将不胜感激一些帮助