JUnit测试用例中的空指针异常

问题描述 投票:-1回答:1

是否有任何方法可以为特定情况编写模拟测试用例。

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"); 
    } 
}

我确实模拟了HttpServeletResponseRegBeanStaffinfo。但是,由于它是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);     
}
java unit-testing junit mockito junit4
1个回答
0
投票

您需要考虑要在这里测试的内容。什么是“输出”?这里的“输出”是该方法正在对您的HttpServletResponse做某事,它正在调用sendRedirect。

您可以使用Mockito.verify验证是否在模拟(或真实)对象上调用了某些方法>

© www.soinside.com 2019 - 2024. All rights reserved.