[英]Mockito: How do unit test void method java mockito?
I have following code I want to unit test: 我有以下代码要进行单元测试:
public class TransformarExcel
{
public TransformarExcel() {
//Constructor
}
public void validarEntero(HttpServletRequest request, HttpServletResponse response, Integer rowCount, String column, String value)
{
if (value.equals("0")) {
generateErrorProcessingFile(request, response, ALERTDANGER, MSGCOLUMNA + column + MSGREGISTRO + rowCount + " no puede ser vacío.");
}
}
public void generateErrorProcessingFile(HttpServletRequest request, HttpServletResponse response, String typeError, String messageError)
{
request.setAttribute("typeMessage", typeError);
request.setAttribute("message", messageError);
try {
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (ServletException|IOException ex) {
Logger.getLogger(ServletTransformarExcelDesembolso.class.getName()).log(Level.SEVERE, null, ex);
}
}
I need to verify that method validarEntero or the method generateErrorProcessingFile are execute, Since both methods do not return anything. 我需要验证方法validarEntero或方法generateErrorProcessingFile是否已执行,因为这两个方法均未返回任何内容。
This is I do: 这是我要做的:
@Test
final void testValidarEntero() throws IOException {
ServletTransformarExcelDesembolso manager = Mockito.mock(ServletTransformarExcelDesembolso.class);
ServletTransformarExcelDesembolso dato = new ServletTransformarExcelDesembolso();
dato.validarEntero(null, null, null, null, "0");
verify(manager, Mockito.timeout(1)).validarEntero(null, null, null, null, "0");}
Thank you :). 谢谢 :)。
You can use mock
or spy
objects to verify expected behaviour. 您可以使用
mock
对象或spy
对象来验证预期的行为。
@Test
final void testValidarEntero() throws IOException {
HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);
Mockito.when(mockRequest.getRequestDispatcher(anyString())).thenReturn(Mockito.mock(RequestDispatcher.class));
ServletTransformarExcelDesembolso dato = new ServletTransformarExcelDesembolso();
dato.validarEntero(mockRequest, null, null, null, "0");
verify(mockRequest, Mockito.times(1)).getRequestDispatcher("index.jsp");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.