Mockito isA()&任何...() [英] Mockito isA() & any...()

查看:184
本文介绍了Mockito isA()&任何...()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

verify(mock, times(1)).myMethod(Matchers.isA(String.class));
verify(mock, times(1)).myMethod(Matchers.anyString());

来自Mockito库的

吗?两者都通过了我的方法,我想知道哪种方法更好".

from the Mockito library? Both pass for my method and I'm wondering which one is "better" to use.

推荐答案

any可以接受类参数并且anyString在名称中指定该参数,"noreferrer> anyString 也会完全忽略该参数,包括其类型.

isA checks that the class matches the expected class. In Mockito 1.x, any, anyObject, and anyString ignore the argument entirely including its type, even though any can take a class parameter and anyString specifies it in the name.

通常,除非您有理由防止传入不兼容的参数,否则您可以坚持使用anyanyString. Mockito样式更喜欢灵活的测试用例,这意味着仅验证您要明确检查的内容,并故意允许其他所有内容都未指定.

Typically, unless you have a reason to guard against an incompatible argument being passed in, you can probably stick with any and anyString. Mockito style prefers flexible test cases, which means verifying only the things that you are explicitly checking, and deliberately allowing everything else to be unspecified.

更新:Mockito提交者 Brice 提供了

UPDATE: Mockito committer Brice has offered some historical background and future direction:

出于历史参考,anyanything的简写别名,当时API强制将其强制转换,并且贡献者和/或提交者考虑将类作为参数来避免这种强制转换,而没有更改此API的语义.但是,此更改最终修改了人们认为该API所做的事情. 此问题将在Mockito 2+中修复

For historical reference, any is a shorthand alias of anything, at that time the API was forcing one to cast, and contributors and/or commiters thought about passing the class as a param to avoid this cast, without changing the semantic of this API. However this change eventually modified what people thought that this API was doing. This will be fixed in mockito 2+

这篇关于Mockito isA()&任何...()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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