0

我有以下功能(功能并不重要):

    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")
    }

代码有效,功能强大,只是测试的模拟现在被打破了。将不胜感激一些帮助

4

1 回答 1

1

Spring retry 在对象周围创建一个代理。

如果有接口,则代理为JDK代理;如果没有,则使用 CGLIB。

Mockito 不能模拟 CGLIB(最终)方法。

于 2021-03-24T14:57:21.507 回答