Java 是否可以在 Kotlin 中使用 Mockito?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30305217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:26:12  来源:igfitidea点击:

Is it possible to use Mockito in Kotlin?

javamockingmockitokotlin

提问by atok

The problem I'm facing is that Matchers.anyObject()returns null. When used to mock method that only accepts non-nullable types it causes a "Should not be null" exception to be thrown.

我面临的问题是Matchers.anyObject()返回null。当用于模拟只接受不可为空类型的方法时,它会导致抛出“不应为空”异常。

`when`(mockedBackend.login(anyObject())).thenAnswer { invocationOnMock -> someResponse }

Mocked method:

模拟方法:

public open fun login(userCredentials: UserCredentials): Response

采纳答案by Sergey Mashkov

There are two possible workarounds:

有两种可能的解决方法:

private fun <T> anyObject(): T {
    Mockito.anyObject<T>()
    return uninitialized()
}

private fun <T> uninitialized(): T = null as T

@Test
fun myTest() {
    `when`(mockedBackend).login(anyObject())).thenAnswer { ... }
}

The other workaround is

另一个解决方法是

private fun <T> anyObject(): T {
    return Mockito.anyObject<T>()
}

@Test
fun myTest() {
    `when`(mockedBackend).login(anyObject())).thenAnswer { ... }
}

Here is some more discussionon this topic, where the workaround is first suggested.

以下是有关此主题的更多讨论,其中首先建议了解决方法。

回答by maxandron

I use verifya lot to make sure the parameters passed to a function are also correct.

我使用verify了很多方法来确保传递给函数的参数也是正确的。

To do this, and still avoid the NPE you can use kotlin's elvis operator. for example: verify(mock).func(same(my_obj) ?: my_obj)

要做到这一点,并且仍然避免 NPE,您可以使用 kotlin 的 elvis 运算符。例如: verify(mock).func(same(my_obj) ?: my_obj)

This way, mockito is satisfied because it actually verifies the variable, and kotlin is satisfied because we pass a non null object.

这样,mockito 就满足了,因为它实际上验证了变量,而 kotlin 满足了,因为我们传递了一个非空对象。

Another thing I stumbled upon was the mockito-kotlin library which solves exactly this issue https://github.com/nhaarman/mockito-kotlin

我偶然发现的另一件事是 mockito-kotlin 库,它正好解决了这个问题 https://github.com/nhaarman/mockito-kotlin

回答by makovkastar

You can use the following helper functions to use Mockito's any(), eq() and capture() matchers in Kotlin:

您可以使用以下辅助函数在 Kotlin 中使用 Mockito 的 any()、eq() 和 capture() 匹配器:

/**
 * Returns Mockito.eq() as nullable type to avoid java.lang.IllegalStateException when
 * null is returned.
 *
 * Generic T is nullable because implicitly bounded by Any?.
 */
fun <T> eq(obj: T): T = Mockito.eq<T>(obj)

/**
 * Returns Mockito.any() as nullable type to avoid java.lang.IllegalStateException when
 * null is returned.
 */
fun <T> any(): T = Mockito.any<T>()

/**
 * Returns ArgumentCaptor.capture() as nullable type to avoid java.lang.IllegalStateException
 * when null is returned.
 */
fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()

See MockitoKotlinHelpers.ktfrom the Android Architecture Blueprints repository by Google.

请参阅Google的 Android 架构蓝图存储库中的MockitoKotlinHelpers.kt

回答by Shamm

For those who need typed any(type: Class<T>)

对于那些需要打字的人 any(type: Class<T>)

    private fun <T> any(type: Class<T>): T = Mockito.any<T>(type)

This would work and the type check also happens!

这会起作用并且类型检查也会发生!