Java 如何使用Mockito验证未使用任何参数组合调用的模拟方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19302207/
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
How to verify mocked method not called with any combination of parameters using Mockito
提问by Dan King
How can I verify that a mocked method was not called at all - with any combination of parameters - using Mockito?
如何使用 Mockito 验证完全没有调用模拟方法 - 使用任何参数组合?
For example I have an object - myObject
- that is using a second, mocked object - myMockedOtherObject
- that has a method - someMethodOrOther(String parameter1, String parameter2)
.
例如,我有一个对象——myObject
它正在使用第二个模拟对象myMockedOtherObject
——它有一个方法—— someMethodOrOther(String parameter1, String parameter2)
。
I want to call myObject.myMethod()
and verify that someMethodOrOther()
doesn'tget called - with anycombination of parameters.
我想调用myObject.myMethod()
并验证someMethodOrOther()
没有被调用 - 使用任何参数组合。
e.g.:
例如:
myObject.doSomeStuff();
verify(myMockedOtherObject, never()).someMethodOrOther();
Except I can't do that, because someMethodOrOther()
requires specific parameters to be supplied.
除非我不能这样做,因为someMethodOrOther()
需要提供特定的参数。
采纳答案by Matt Lachman
You can accomplish what you want with Mockito's argument matchers:
您可以使用 Mockito 的参数匹配器完成您想要的操作:
myObject.doSomeStuff();
verify(myMockedOtherObject, never()).someMethodOrOther(
Mockito.anyString(),
Mockito.anyString()
);
You can make that a little less verbose with a static import like you have for verify
and never
.
您可以像 forverify
和那样使用静态导入来减少冗长never
。
回答by Dawood ibn Kareem
You need to use argument matchers to do stuff like this. You supply an argument matcher to correspond to every parameter in your method, but you must make sure that you pick one that has the right type. All of the ones you are likely to need are listed at http://docs.mockito.googlecode.com/hg/latest/org/mockito/Matchers.html.
你需要使用参数匹配器来做这样的事情。您提供一个参数匹配器以对应于您方法中的每个参数,但您必须确保选择一个具有正确类型的参数。您可能需要的所有内容都在http://docs.mockito.googlecode.com/hg/latest/org/mockito/Matchers.html中列出。
Suppose your method is
假设你的方法是
public void myMethod(
String text, int count, MyClass something, List<MyClass> someList) {
// ...
}
Your verify statement might look like this.
您的验证语句可能如下所示。
verify(myMock, never()).myMethod(
anyString(), anyInt(), any(MyClass.class), anyListOf(MyClass.class));
Some of the matchers that you're likely to need are -
您可能需要的一些匹配器是 -
anyInt(), anyLong(), anyShort(), anyBoolean(), anyByte(), anyChar(), anyFloat(), anyDouble()
- These match either the primitive version or the object version of each of these types. In my example, I've usedanyInt()
to match anint
, but it will also match anInteger
.any(XXX.class)
- This will match any object type at all. In my example, I've used it to match aMyClass
.anyString()
- This is an alternative way of writingany(String.class)
anyListOf(XXX.class), anySetOf(XXX.class), anyMapOf(XXX.class, XXX.class)
- These are good for matching the standard generic collection types. In my example, I've usedanyListOf
to match theList<MyClass>
.
anyInt(), anyLong(), anyShort(), anyBoolean(), anyByte(), anyChar(), anyFloat(), anyDouble()
- 这些匹配每种类型的原始版本或对象版本。在我的示例中,我曾经anyInt()
匹配一个int
,但它也会匹配一个Integer
。any(XXX.class)
- 这将完全匹配任何对象类型。在我的示例中,我使用它来匹配MyClass
.anyString()
- 这是另一种写作方式any(String.class)
anyListOf(XXX.class), anySetOf(XXX.class), anyMapOf(XXX.class, XXX.class)
- 这些有利于匹配标准的泛型集合类型。在我的示例中,我曾经anyListOf
将List<MyClass>
.
There are a handful of others, and I strongly recommend having a brief skim through the Javadoc. But these are the ones that you are most likely to use with never()
.
还有一些其他的,我强烈建议您简要浏览一下 Javadoc。但这些是您最有可能与never()
.
回答by manindra naresh
More Clear way of presenting the solution
更清晰的呈现解决方案的方式
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
//Testing scenario
verify(mockObject, never()).someMethod(mockParam1, MockParam2);