Java 使用无效方法的doNothing方法时出现未完成存根异常
以下代码导致UnfinishedStubingExceptionJava 使用无效方法的doNothing方法时出现未完成存根异常,java,junit,mockito,powermockito,Java,Junit,Mockito,Powermockito,以下代码导致UnfinishedStubingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString()); ve
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运行时没有任何错误,但它应该会失败,因为我在
when
和verify
itsMember\u Servicing\u Email\u Update123
中通过了Member\u Servicing\u Email\u Update
PowerMockito.doNothing().when(WidgetHelper.class)代码>
当您创建一个mock all-it方法时,默认调用doNothing
。因此,您不需要明确声明它
但是,如果要声明行为,则需要命名相关的方法。给定行中缺少的。错误是由以下行引起的,这是无效语法:
PowerMockito.doNothing().when(WidgetHelper.class)代码>
当您创建一个mock all-it方法时,默认调用doNothing
。因此,您不需要明确声明它
但是,如果要声明行为,则需要命名相关的方法。给定行中缺少的。能否显示invokeAuditService
方法签名?它是空的/静态的吗?看,我不明白。我无法将答案与我的代码联系起来。正确的方法是模拟静态(或者根本不使用静态…)你能显示invokeAuditService
方法签名吗?它是空的/静态的吗?看,我不明白。我无法将答案与我的代码联系起来。用正确的方法模拟静态(或者根本不使用静态…)