java Mockito 验证被监视对象方法的返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16967552/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:33:07  来源:igfitidea点击:

Mockito verify the return of a spied object method

javamockito

提问by David

I know you can verify the times a spied object's method was called. Can you verify the result of the method call?

我知道你可以验证被监视对象的方法被调用的次数。你能验证方法调用的结果吗?

Something like the following?

像下面这样的?

verify(spiedObject, didReturn(true)).doSomething();

采纳答案by John B

To verify the number of times it was invoked, use verify(spiedObject, times(x)).doSomething().

要验证它被调用的次数,请使用verify(spiedObject, times(x)).doSomething()

You should NOT be verifying the value returned from the spied object. It is not the object under test so why verify what it returns. Instead verify the behavior of the object under test in response to the value returned from the spy.

您不应该验证从被监视对象返回的值。它不是被测对象,所以为什么要验证它返回的内容。而是验证被测对象的行为以响应从间谍返回的值。

Also, if you don't KNOW what value will be returned by the spied object it would be better to use a mock instead of a spy.

此外,如果您不知道被监视的对象将返回什么值,最好使用模拟而不是间谍。