UnfinishedStubbingException when working with doNothing method for void methods

荒凉一梦 提交于 2020-01-16 19:04:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!