英文:
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 <T> anyObject(): T {
Mockito.any<T>()
return uninitialized()
}
@Suppress("UNCHECKED_CAST")
fun <T> 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。该库通过尝试创建实际实例来解决此问题。
inline fun <reified T : Any> any(): T {
return Mockito.any(T::class.java) ?: 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 <reified T : Any> any(): T {
return Mockito.any(T::class.java) ?: createInstance()
}
createInstance() is implemented like this:
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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论