I am facing some error while writing test case for Request dispatcher.
My class
@Override
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain)
throws IOException, ServletException
{
if(isMockAccountEnabled())
{
HttpServletRequest req = (HttpServletRequest)request;
String reqUrl = req.getRequestURI();
ApiUserDetails userDetails = userBean.getUserDetails();
HttpSession session = req.getSession();
if(isThisTestAccount(reqUrl, session))
{
log.info(userDetails);
log.debug("Entering Test acount flow for the request "+reqUrl);
RequestDispatcher dispatcher = req.getRequestDispatcher("/mock/" + EnumService.returnMockService(reqUrl));
dispatcher.forward(request, resp);
}
}
}
Test case written
@Mock
private FilterChain chain;
@InjectMocks
private MockAccountFilter mockAccountFilter = new MockAccountFilter();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpSession session = new MockHttpSession();
@Test
public void filterRequestMockFirst()
throws Exception
{
MockRequestDispatcher dispatcher =new MockRequestDispatcher("/mock/ABCTEST");
when(request.getRequestDispatcher("/mock/ABCTEST")).thenReturn(dispatcher);
request.setRequestURI("/check/employee/123456/false");
mockAccountFilter.doFilter(request, response, chain);
Assert.assertTrue(request.getRequestURI().contains("/mock/ABCTEST"));
}
Error
when() requires an argument which has to be 'a method call on a mock'.
Can some one tell me the exact way of writing this test case.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…