我有一个类似于此的Filter类。
我的班级
@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.debug("Entering Test acount flow for the request " + reqUrl);
RequestDispatcher dispatcher = req.getRequestDispatcher("/mock/" + EnumService.returnMockService(reqUrl));
dispatcher.forward(request, resp);
}
}
}
我想把JUNIT测试用例写进去。
我尝试过类似的事。
写的测试用例
@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"));
}
我是编写测试用例的新手。
我无法以适当的方式弄清楚它是怎么做的。
我收到错误消息
错误
when() requires an argument which has to be 'a method call on a mock'.
有人可以建议我为上述课程编写测试用例的最佳方式。