问题
The below code is causing UnfinishedStubbingException
PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString());
verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update1"), Matchers.eq(jsonObject), anyString());
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at ....
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
what I'm I missing here? Below is the method signature of invokeAuditService
public static void invokeAuditService(HttpServletRequest request, Date serviceCallTime, String response,
String activityKey, JSONObject detailsReplaceVal, String pmAccountId){
AuditLogUtils.invokeAuditService(request, date, response, activityKey, json, someString);
}
I did this:
PowerMockito.mockStatic(WidgetHelper.class);
PowerMockito.doNothing().when(WidgetHelper.class);
WidgetHelper.invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(),
Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString());
verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(),
Matchers.eq("Member_Servicing_Email_Update123"), Matchers.eq(jsonObject), anyString());
Junit runs without any error but it supposed to fail since I have passed Member_Servicing_Email_Update
in when
and in verify
its Member_Servicing_Email_Update123
回答1:
The error is caused by the following line, which is invalid syntax:
PowerMockito.doNothing().when(WidgetHelper.class);
When you create a mock all it method calls default to doNothing
. So you do not need to declare it explictly.
However if you want to declare a behaviour you need to name the related method. Which is missing in the given line.
来源:https://stackoverflow.com/questions/59643836/unfinishedstubbingexception-when-working-with-donothing-method-for-void-methods