如何使用 Junit/Mockito 对该 void 方法进行单元测试
我有以下空隙方法:
public void checkInvalidEarlyAccessKey(HippoBean hippoBean, HstRequest hstRequest, HstResponse hstResponse, HttpServletRequest request) {
if (StringUtils.isNotBlank(hippoBean.getSingleProperty(EARLY_ACCESS_KEY)) && !hippoBean.getSingleProperty(EARLY_ACCESS_KEY).equals(request.getParameter(PARAMETER_KEY))) {
LOGGER.debug("Early access key is set and null or wrong key is being used. Redirecting to 404 error code");
hstResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
HstResponseUtils.sendRedirect(hstRequest, hstResponse, ERROR_404_PATH);
}
}
如何使用Junit/Mockito进行测试?我是否应该测试Hstresponse状态确实是404,以确保if语句是正确的?您能提供一个代码示例吗?最好的做法是什么?
谢谢
I have the following void method:
public void checkInvalidEarlyAccessKey(HippoBean hippoBean, HstRequest hstRequest, HstResponse hstResponse, HttpServletRequest request) {
if (StringUtils.isNotBlank(hippoBean.getSingleProperty(EARLY_ACCESS_KEY)) && !hippoBean.getSingleProperty(EARLY_ACCESS_KEY).equals(request.getParameter(PARAMETER_KEY))) {
LOGGER.debug("Early access key is set and null or wrong key is being used. Redirecting to 404 error code");
hstResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
HstResponseUtils.sendRedirect(hstRequest, hstResponse, ERROR_404_PATH);
}
}
How can I test this with Junit/Mockito? Should I test that the hstResponse status is indeed a 404 to ensure the if statement is true? Could you provide a code example? What is the best practice?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您的期望。下面的链接包括
模拟void
方法的有用和批准的答案。您可以开始阅读我提供的链接,并决定您将验证或断言的内容。如何使用Mockito 模拟Void方法
It depends on what you expect. Below link includes useful and approved answers to
mock void
methods. You can start to read link I provided and decide what you will verify or assert.How to mock void methods with Mockito