使用Mockito与Kotlin一起使用时,如何避免”any() must not be null”的问题?

huangapple go评论73阅读模式
英文:

When using Mockito with Kotlin how do you get around any() must not be null?

问题

在尝试模拟函数调用 getInventoryList(object, string, int, object, int) 时,我发现我会持续遇到错误 ArgumentMatchers.any() 必须不为 null。

以下是我尝试过的一些解决方案。
MockitoHelper

object MockitoHelper {
    fun <T> anyObject(): T {
        Mockito.any<T>()
        return uninitialized()
    }
    @Suppress("UNCHECKED_CAST")
    fun <T> uninitialized(): T = null as T
}

已弃用的 anyObject() 函数

ArgumentMatchers.anyObject()

any(class : T)

ArgumentMatchers.any(Object::class.java)

上述任何解决方案都未能解决这个特定的问题。每个解决方案都返回类似的“必须不为 null”错误。

英文:

While trying to mock up a function call getInventoryList(object, string, int, object, int) I found that I would continually encounter the error ArgumentMatchers.any() must not be null.

Here are some solutions I tried.
The MockitoHelper

object MockitoHelper {
fun &lt;T&gt; anyObject(): T {
    Mockito.any&lt;T&gt;()
    return uninitialized()
}
@Suppress(&quot;UNCHECKED_CAST&quot;)
   fun &lt;T&gt; uninitialized(): T =  null as T
}

The any object deprecated function

ArgumentMatchers.anyObject()

The any(class : T)

ArgumentMatchers.any(Object::class.java)

None of the above worked for this particular problem. Each of them came back with a similar must not be null error

答案1

得分: 7

以下是要翻译的内容:

这是问题的解决方案。 告诉 Kotlin,如果返回为 null,则使用对象的初始化版本。

ArgumentMatchers.any(Object::class.java) ?: Object()

这对我的解决方案起效果了。

英文:

Here is the solution to the problem. Tell Kotlin that if there is a null return to use an initialized version of the object.

ArgumentMatchers.any(Object::class.java) ?: Object()

This worked for my solution

答案2

得分: 1

以下是已翻译的内容:

使用Mockito与Kotlin一起使用时会存在一些问题。

这个小型库解决了其中的主要问题:https://github.com/nhaarman/mockito-kotlin

来自维基页面

此外,Mockito对诸如any()之类的方法调用返回null值,这可能会导致将它们传递给非可空参数时引发IllegalStateException。该库通过尝试创建实际实例来解决此问题。

这是它们如何实现any的方式

inline fun <reified T : Any> any(): T {
    return Mockito.any(T::class.java) ?: createInstance()
}

createInstance() 的实现如下:

inline fun <reified T : Any> createInstance(): T {
    return when (T::class) {
        Boolean::class -> false as T
        Byte::class -> 0.toByte() as T
        Char::class -> 0.toChar() as T
        Short::class -> 0.toShort() as T
        Int::class -> 0 as T
        Long::class -> 0L as T
        Float::class -> 0f as T
        Double::class -> 0.0 as T
        else -> createInstance(T::class)
    }
}

fun <T : Any> createInstance(kClass: KClass<T>): T {
    return castNull()
}
英文:

There are a number of rough edges when using mockito with Kotlin.

This small library takes care of the major ones: https://github.com/nhaarman/mockito-kotlin

From the wiki:

> Furthermore, Mockito returns null values for calls to method like any(), which can cause IllegalStateException when passing them to non-nullable parameters. This library solves that issue by trying to create actual instances to return.

This is how they implement any

inline fun &lt;reified T : Any&gt; any(): T {
    return Mockito.any(T::class.java) ?: createInstance()
}

createInstance() is implemented like this:

inline fun &lt;reified T : Any&gt; createInstance(): T {
    return when (T::class) {
        Boolean::class -&gt; false as T
        Byte::class -&gt; 0.toByte() as T
        Char::class -&gt; 0.toChar() as T
        Short::class -&gt; 0.toShort() as T
        Int::class -&gt; 0 as T
        Long::class -&gt; 0L as T
        Float::class -&gt; 0f as T
        Double::class -&gt; 0.0 as T
        else -&gt; createInstance(T::class)
    }
}

fun &lt;T : Any&gt; createInstance(kClass: KClass&lt;T&gt;): T {
    return castNull()
}

huangapple
  • 本文由 发表于 2020年10月13日 01:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64322642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定