【问题标题】:Kotlin Argument Matcher failing with exceptionKotlin 参数匹配器因异常而失败
【发布时间】:2021-12-13 21:14:22
【问题描述】:

我有以下代码在参数不匹配错误中失败

       mockSpotAccessor.doWithRetry(eq({
            mockSpotAccessor.asyncNestedAggregation(any(), any(), any(), any(), any()) }), eq(5))

        verify(mockSpotAccessor, times(1)).doWithRetry(eq({
            mockSpotAccessor.asyncNestedAggregation(any(), any(), any(), any(), any()) }), eq(5))

预期 spotAccessor.doWithRetry( (callRetryForAsyncCalls$2) () -> java.util.concurrent.CompletableFuture<kotlin.collections.List<com.amazon.noblearsenallambda.AggregationResultObject>>, 5 );

实际 spotAccessor.doWithRetry( (callRetryForAsyncCalls$answer$1) () -> java.util.concurrent.CompletableFuture<kotlin.collections.List<com.amazon.noblearsenallambda.AggregationResultObject>>, 0 );

我不确定,因为我是第一次使用 mockito

【问题讨论】:

    标签: kotlin mockito


    【解决方案1】:
    1. 即使内容相同,也不能期望这两个 lambdas 彼此是 eq (equals),除非它们引用完全相同的对象引用。

    2. 当使用匹配器时,匹配器只能直接在whenverify 的调用中用于whenverify 调用的所有参数。因为您描述的是 lambda,所以对 any() 的调用不会作为对 whenverify 的调用的一部分发生,因此您不应使用匹配器。

    最好的办法是使用 any() 代替整个 Unit 或回调 lambda,或者可能使用 ArgumentCaptor 来捕获 lambda,然后在受控条件下调用 lambda,verify 它是如您所愿调用。

    【讨论】:

    • 我的 lambda 函数定义是 fun <T> doWithRetry(action: () -> CompletableFuture<T>, retryCount: Int): CompletableFuture<T> {}。如果我通过了任何我得到错误 type inference failed: Not enough information to infer parameter T in fun <T> doWithRetry ( action: () → CompletableFuture<T>, retryCount: Int ) : CompletableFuture<T> Please specify it explicitly.
    • @SakshiGarg 这是一个稍微不同的问题,但你可以打电话给<List<AggregationResultObject>>doWithRetry(/*...*/)as described here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2015-12-22
    • 2020-11-14
    • 2018-03-12
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多