【发布时间】: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 运行时没有任何错误,但它应该会失败,因为我在 when 和 verify 中通过了 Member_Servicing_Email_Update 和 Member_Servicing_Email_Update123
【问题讨论】:
-
你能显示
invokeAuditService方法签名吗?它是无效/静态的吗? -
我不明白。我无法将答案与我的代码联系起来。
-
以正确的方式模拟静态github.com/powermock/powermock/wiki/mockstatic(或者根本不使用静态......)
标签: java junit mockito powermockito