【问题标题】:UnfinishedStubbingException when working with doNothing method for void methods使用 void 方法的 doNothing 方法时出现 UnfinishedStubbingException
【发布时间】:2020-01-08 10:25:43
【问题描述】:

下面的代码导致了 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!

我在这里错过了什么? 下面是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);
    }

我这样做了:

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 运行时没有任何错误,但它应该会失败,因为我在 whenverify 中通过了 Member_Servicing_Email_UpdateMember_Servicing_Email_Update123

【问题讨论】:

标签: java junit mockito powermockito


【解决方案1】:

错误是由以下行引起的,它是无效的语法: PowerMockito.doNothing().when(WidgetHelper.class);

当你创建一个 mock all it 方法调用默认为doNothing。所以你不需要显式声明它。

然而,如果你想声明一个行为,你需要命名相关的方法。给定行中缺少哪个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多