Saturday, 20 May 2017

java - Mockito error : "Wanted but not invoked:.. However, there were other interactions with this mock"




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);



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);?



Additional Detail



Definition of 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();
}

Answer



Being new to Mockito I didnt quite realise what I was looking for. But this is precisely what I wanted.
Hope this helps anyone else stuck with a similar problem.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...