ProjectSetupInterceptor extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if ((projectService.getActiveProject() == null && !request.getRequestURI().startsWith(request.getContextPath() + redirect) && !request.getRequestURI().startsWith(redirect) && !isExcluded(request.getRequestURI(), request.getContextPath()))) { log.debug("Intercept " + request.getRequestURI() + " as project home is not set properly"); log.debug("Redirecting to " + request.getContextPath() + redirect); response.sendRedirect(request.getContextPath() + redirect); return false; } return true; }  boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler); String getRedirect(); void setRedirect(String redirect); String[] getExcludes(); void setExcludes(String[] excludes); void setProjectService(ProjectService projectService);  }
@Test(dataProvider = "requests") public void testPreHandle(String requestUri, String contextPath, Project activeProject, boolean shouldRedirect) throws Exception { ProjectSetupInterceptor interceptor = new WebConfig().getProjectSetupInterceptor(); interceptor.setProjectService(projectService); reset(request, response); when(projectService.getActiveProject()).thenReturn(activeProject); when(request.getRequestURI()).thenReturn(requestUri); when(request.getContextPath()).thenReturn(contextPath); boolean continueHandle = interceptor.preHandle(request, response, null); Assert.assertEquals(continueHandle, !shouldRedirect); if (shouldRedirect) { verify(response).sendRedirect(interceptor.getRedirect()); } }