与void方法一起使用doNothing方法时,出现UnfinishedStubbingException [英] UnfinishedStubbingException when working with doNothing method for void methods

查看:1454
本文介绍了与void方法一起使用doNothing方法时,出现UnfinishedStubbingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码导致UnfinishedStubbingException

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!

我在这里想念什么? 下面是invokeAuditService的方法签名

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

我这样做了:

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_Update123

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

推荐答案

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

The error is caused by the following line, which is invalid syntax: PowerMockito.doNothing().when(WidgetHelper.class);

创建模拟所有it方法时,默认调用为doNothing.因此,您无需明确声明它.

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.

这篇关于与void方法一起使用doNothing方法时,出现UnfinishedStubbingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆