我在为请求调度程序编写测试用例时遇到了一些错误。 我班编写JUnit测试用例请求调度程序时出错
@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);
}
}
}
测试用例编写
@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'.
有人能告诉我写这篇测试案例的具体方式。
MockHttpXxx类不是Mockito嘲笑。它们是实现servlet接口的简单POJO,但可以通过setter进行配置。模拟请求将返回一个模拟调度器AFAIK。而且你在测试中并没有使用调度程序,所以你可以删除该行。 –