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

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

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

  1. object MockitoHelper {
  2. fun <T> anyObject(): T {
  3. Mockito.any<T>()
  4. return uninitialized()
  5. }
  6. @Suppress("UNCHECKED_CAST")
  7. fun <T> uninitialized(): T = null as T
  8. }

已弃用的 anyObject() 函数

  1. ArgumentMatchers.anyObject()

any(class : T)

  1. 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

  1. object MockitoHelper {
  2. fun &lt;T&gt; anyObject(): T {
  3. Mockito.any&lt;T&gt;()
  4. return uninitialized()
  5. }
  6. @Suppress(&quot;UNCHECKED_CAST&quot;)
  7. fun &lt;T&gt; uninitialized(): T = null as T
  8. }

The any object deprecated function

  1. ArgumentMatchers.anyObject()

The any(class : T)

  1. 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,则使用对象的初始化版本。

  1. 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.

  1. 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的方式

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

createInstance() 的实现如下:

  1. inline fun <reified T : Any> createInstance(): T {
  2. return when (T::class) {
  3. Boolean::class -> false as T
  4. Byte::class -> 0.toByte() as T
  5. Char::class -> 0.toChar() as T
  6. Short::class -> 0.toShort() as T
  7. Int::class -> 0 as T
  8. Long::class -> 0L as T
  9. Float::class -> 0f as T
  10. Double::class -> 0.0 as T
  11. else -> createInstance(T::class)
  12. }
  13. }
  14. fun <T : Any> createInstance(kClass: KClass<T>): T {
  15. return castNull()
  16. }
英文:

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

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

createInstance() is implemented like this:

  1. inline fun &lt;reified T : Any&gt; createInstance(): T {
  2. return when (T::class) {
  3. Boolean::class -&gt; false as T
  4. Byte::class -&gt; 0.toByte() as T
  5. Char::class -&gt; 0.toChar() as T
  6. Short::class -&gt; 0.toShort() as T
  7. Int::class -&gt; 0 as T
  8. Long::class -&gt; 0L as T
  9. Float::class -&gt; 0f as T
  10. Double::class -&gt; 0.0 as T
  11. else -&gt; createInstance(T::class)
  12. }
  13. }
  14. fun &lt;T : Any&gt; createInstance(kClass: KClass&lt;T&gt;): T {
  15. return castNull()
  16. }

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:

确定