Android Mockito.verify说参数不同!打印相同的内容 [英] Android Mockito.verify says Argument(s) are different! with printing same content
问题描述
情况
我有一个Tracker
的接口,该接口具有以下方法:
I have an interface of a Tracker
which has this method:
fun trackEvent(event: String, args: Bundle? = null)
我想验证是否使用特定的event
调用了此方法.
在内部测试对象使用Bundle
对象调用此方法.
所有事件均指定为
I want to verify, that this method is called with a specific event
.
Internally object that being tested call this method with Bundle
object.
All the events are specified as
companion object {
const val EVENT = "EVENT"
}
问题
此验证失败,测试失败:
The test fails with this verification:
Mockito.verify(tracker).trackEvent(Tracker.EVENT)
带有消息:
参数不同!通缉: tracker.trackEvent("EVENT",null); ...
Argument(s) are different! Wanted: tracker.trackEvent("EVENT", null); ...
实际调用具有不同的参数: tracker.trackEvent("EVENT",null); ...
Actual invocation has different arguments: tracker.trackEvent("EVENT", null); ...
有很多解决方案,正在使用
Mockito.eq()
,Mockito.refEq()
,ArgumentMatchers
,Captures
等.它们对我都不起作用,给出相同的结果或NullPointerException
There are many solutions, which are using
Mockito.eq()
, Mockito.refEq()
, ArgumentMatchers
, Captures
, etc. None of them worked for me, giving the same or NullPointerException
推荐答案
对于那些将面临相同问题的人,解决方案是您需要添加
build.gradle
文件中的testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
.更多信息,在这里
https://github.com/nhaarman/mockito-kotlin .
For those who will face same problem, the solution is that you need to add
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
in your build.gradle
file. More info, here
https://github.com/nhaarman/mockito-kotlin.
问题是,与Kotlin
不同,在Java
中所有类默认情况下都可以为空.尽管嘲笑被设计为与Java
一起使用,但上面的库使用Mockito与Kotlin
一起增加了支持.
The thing is that in Java
all classes are nullable by default, unlike Kotlin
. While mockito is designed to be used with Java
, the library from above adds support using Mockito with Kotlin
.
因此,此特定测试的解决方案
So, the solution to this particular test
Mockito.verify(tracker).trackEvent(Tracker.EVENT)
是
Mockito.verify(tracker).trackEvent(eq(Tracker.ADD_TRANSACTION), any())
其中eq()
和any()
是com.nhaarman.mockito_kotlin
的功能.
此处有更多信息: https://stackoverflow.com/a/38722935/3569545
这篇关于Android Mockito.verify说参数不同!打印相同的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!