Android Mockito.verify说参数不同!打印相同的内容 [英] Android Mockito.verify says Argument(s) are different! with printing same content

查看:238
本文介绍了Android Mockito.verify说参数不同!打印相同的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我有一个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()ArgumentMatchersCaptures等.它们对我都不起作用,给出相同的结果或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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆