Java Mockito 错误:“需要但未调用:.. 但是,与此模拟还有其他交互”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18217285/
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
Mockito error : "Wanted but not invoked:.. However, there were other interactions with this mock"
提问by seeker
Im getting the following error, when I try to mock overloaded methods by passing casted values .
当我尝试通过传递强制转换值来模拟重载方法时,我收到以下错误。
For example in order to mock
ABCClass.logWarn(Logger log,String , String description, Throwable e);
例如为了模拟
ABCClass.logWarn(Logger log,String , String description, Throwable e);
Im doing
我正在做
`ABCClass.logWarn(null,WarningString, description, (Throwable)null);
...\ The rest of the methods are standard...
verify(event).setStatus((Throwable)null);//**Line 76**
But when I run my test cases Im getting the following error
但是当我运行我的测试用例时,我收到以下错误
ABCClassTest.testLogWarn:76
Wanted but not invoked:
MockEvent.setStatus(null);
-> at com.path.ABCClassTest.testLogWarn(ABCClassTest.java:76)
However, there were other interactions with this mock:.....
Why is setStatus(null)
expected to be called, even after specifically calling the
setStatus((Throwable)null);
?
为什么setStatus(null)
预计会被调用,即使在专门调用了
setStatus((Throwable)null);
?
Additional Detail
附加细节
Definition of logWarn
logWarn 的定义
private static void logWarn(String eventType, String eventName, String errMsg, Throwable e) {
AnEvent event = AnEventFactory.create(eventType);
event.setName(eventName);
if(e!=null)
event.setStatus(e);//so this is never called if the throwable is null.
//How do I modify the verify behavior?
/*
Bleh */
event.completed();
}
采纳答案by seeker
回答by Dawood ibn Kareem
Casting doesn't change the object that a variable refers to. It just makes the compiler not complain when you use the variable in a way that doesn't match its type. So you really are passing null
into setStatus
following your verify
.
转换不会改变变量引用的对象。当您以与其类型不匹配的方式使用变量时,它只会使编译器不会抱怨。所以你真的是null
在setStatus
关注你的verify
.
Of course, if you're asking why setStatus
isn't actually called by the code that you're testing, you'd need to post it before anyone can tell you.
当然,如果你问为什么setStatus
你正在测试的代码实际上没有调用,你需要在任何人告诉你之前发布它。