我想模拟或存根,我是单元测试的新手。
我有以下方法。这是方法HttpServletRequest请求
的参数Request.getSession()。setAttribute(“ email”,“ [email protected]”);
我确实尝试过doCallRealMethod,但我正在学习。如何使用Mockito对上述方法进行单元测试?
我想验证该方法发送或插入这两个参数。
假设您必须测试一些servlet,我建议采用这种方法
HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
HttpSession session = Mockito.mock(HttpSession.class);
Mockito.doReturn(session).when(req).getSession();
myHttpServlet.service(req, res);
Mockito.verify(req, Mockito.times(1)).getSession();
Mockito.verify(session, Mockito.times(1)).setAttribute(
ArgumentMatchers.eq("email"), ArgumentMatchers.eq("[email protected]"));
Mockito.verifyNoMoreInteractions(session, req);