[英]How to JUnit for method which is static and returns void
How do I write JUnit for the below method?如何为以下方法编写 JUnit?
public static void invokeAuditService(HttpServletRequest request, Date serviceCallTime, String response,
String activityKey, JSONObject detailsReplaceVal, String pmAccountId){
AuditLogUtils.invokeAuditService(request, serviceCallTime, response, activityKey, detailsReplaceVal, pmAccountId);
}
My code:我的代码:
@Test
public void testInvokeAuditService() {
PowerMockito.doNothing().when(WidgetHelper.class);
WidgetHelper.invokeAuditService(Matchers.anyObject(), Matchers.anyObject(), Matchers.anyObject(),
Matchers.anyObject(), Matchers.anyObject(), Matchers.anyObject());
}
I am getting compiler error: The method invokeAuditService(HttpServletRequest, Date, String, String, JSONObject, String) in the type WidgetHelper is not applicable for the arguments (Object, Object, Object, Object, Object, Object) I am getting compiler error: The method invokeAuditService(HttpServletRequest, Date, String, String, JSONObject, String) in the type WidgetHelper is not applicable for the arguments (Object, Object, Object, Object, Object, Object)
Updated code:更新代码:
@Test
public void testInvokeAuditService() {
PowerMockito.doNothing().when(WidgetHelper.class);
WidgetHelper.invokeAuditService(servletRequest, date, "abc", "xyz",json,
"123");
verifyStatic(WidgetHelper.class, Mockito.times(1));
WidgetHelper.invokeAuditService(servletRequest, date, "abc", "xyz",json,
"123");
}
This says: "Actually there were zero interactions with this mock" for the line after verifystatic.这表示:verifystatic 之后的行“实际上与这个模拟的交互为零”。
You are passing all the values as Object
in invokeAuditService
method parameter.您在
invokeAuditService
方法参数中将所有值作为Object
传递。 So you are getting this compiler error.所以你得到这个编译器错误。
For example , you should pass the value of serviceCallTime, response, activityKey, detailsReplaceVal, pmAccountId
例如,您应该传递
serviceCallTime, response, activityKey, detailsReplaceVal, pmAccountId
的值
If you are not having value for all the parameters send the value as null
value, but the null
value should be handle in invokeAuditService
method to avoid NullpointerException
.如果您没有所有参数的值,请将值作为
null
值发送,但null
值应在invokeAuditService
方法中处理以避免NullpointerException
。
change the code like below更改如下代码
@Test
public void testInvokeAuditService() {
PowerMockito.doNothing().when(WidgetHelper.class);
WidgetHelper.invokeAuditService(null, null, null, null, null, null);
}
Now your compiler error will resolve and maybe you will face NullpointerException
in runtime现在您的编译器错误将得到解决,也许您将在运行时遇到
NullpointerException
This worked:这有效:
@Test
public void testInvokeAuditServiceWithSuccessFlagAndResponse() {
PowerMockito.doNothing().when(WidgetHelper.class);
WidgetHelper.invokeAuditService(servletRequest, date, "abc", json,
"123", "Y");
verifyStatic(WidgetHelper.class, Mockito.times(1));
}
I was actually asking Mockito to verify an interaction before the interaction has occurred.我实际上是在要求 Mockito 在交互发生之前验证交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.