是否有任何方法可以为特定情况编写模拟测试用例。
public void saveStaffInfo(HttpServletResponse response, RegBean regdata, Staffinfo staffType, boolean status)
throws IOException {
if (status)
{
boolean status1 = staffType.insertlogin(regdata);
if(status1) {
response.sendRedirect("registration.jsp?status=success");
}
else {
response.sendRedirect("registration.jsp?status=login_table_error");
}
} else {
response.sendRedirect("registration.jsp?status=failed");
}
}
我确实模拟了HttpServeletResponse
,RegBean
,Staffinfo
。但是,由于它是void类型,所以我不能使用doReturn()。when(mockedMethod)。(someMethod)。那么如何测试这些线路?我还需要代码覆盖率。我对此很陌生。测试用例
@Test
public void testSaveStaffInfo() throws IOException, ServletException{
boolean status =true;
// System.out.println(iStaffInfo.insertlogin(regdata));
Mockito.when(iStaffInfo.insertlogin(regdata)).thenReturn(Boolean.TRUE );
reg.saveStaffInfo(response, regdata, iStaffInfo,status);
}
您需要考虑要在这里测试的内容。什么是“输出”?这里的“输出”是该方法正在对您的HttpServletResponse做某事,它正在调用sendRedirect。
您可以使用Mockito.verify验证是否在模拟(或真实)对象上调用了某些方法>