Mockito.anyString()在Kotlin中与NPE一起崩溃 [英] Mockito.anyString() crashes with NPE in Kotlin
问题描述
我正在使用espresso,我想为内容解析器创建模拟响应.
I am using espresso and I want to create a mock response for the content resolver.
当我使用时:
`when`(context.activity.contentResolver.query(
ArgumentMatchers.isA(Uri::class.java),
ArgumentMatchers.isA(Array<String>::class.java),
ArgumentMatchers.anyString(), null, null))
.thenReturn(matrixCursor)
我得到了错误:
java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128)
在这种情况下是否可以为内容解析器创建模拟响应?
Is it possible to create the mock response for the content resolver in such cases?
推荐答案
这看起来像是可空性问题.Mockito在Kotlin中对于可为null的类型不能很好地工作.
That looks like a nullability issue. Mockito does not work that well with nullable types in Kotlin.
有一个库可以处理此问题: https://github.com/nhaarman/mockito-科特林
There is a library that handles this problem: https://github.com/nhaarman/mockito-kotlin
I'd suggest you to check anyOrNull() here and try something like
ArgumentMatchers.anyOrNull<String>(), isNull(), isNull()
阅读这篇文章可以完全理解问题以及如何处理任何不同的情况.
Read this article to gain full understanding of the problem and how to handle it any different scenarios.
这篇关于Mockito.anyString()在Kotlin中与NPE一起崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!