### Question:
ODataServlet extends HttpServlet { @Override protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { ODataServiceFactory serviceFactory = getServiceFactory(req); if(serviceFactory == null) { throw new ODataRuntimeException("Unable to get Service Factory. Check either '" + ODataServiceFactory.FACTORY_LABEL + "' or '" + ODataServiceFactory.FACTORY_INSTANCE_LABEL + "' config."); } String xHttpMethod = req.getHeader("X-HTTP-Method"); String xHttpMethodOverride = req.getHeader("X-HTTP-Method-Override"); if (xHttpMethod != null && xHttpMethodOverride != null) { if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) { ODataExceptionWrapper wrapper = new ODataExceptionWrapper(req, serviceFactory); createResponse(resp, wrapper.wrapInExceptionResponse( new ODataBadRequestException(ODataBadRequestException.AMBIGUOUS_XMETHOD))); return; } } if (req.getPathInfo() != null) { handle(req, resp, xHttpMethod, xHttpMethodOverride, serviceFactory); } else { handleRedirect(req, resp, serviceFactory); } }    }### Answer:
@Test public void handleRedirectWoServletPath() throws Exception { ODataServlet servlet = new ODataServlet(); prepareServlet(servlet); prepareRequest(reqMock, "/context-path", null); servlet.service(reqMock, respMock); Mockito.verify(respMock).setStatus(HttpStatusCodes.TEMPORARY_REDIRECT.getStatusCode()); Mockito.verify(respMock).setHeader(HttpHeaders.LOCATION, "/context-path/"); }
@Test public void handleRedirectWoContextPath() throws Exception { ODataServlet servlet = new ODataServlet(); prepareServlet(servlet); prepareRequest(reqMock, null, "/servlet-path"); servlet.service(reqMock, respMock); Mockito.verify(respMock).setStatus(HttpStatusCodes.TEMPORARY_REDIRECT.getStatusCode()); Mockito.verify(respMock).setHeader(HttpHeaders.LOCATION, "/servlet-path/"); }
@Test public void handleRedirectWoPath() throws Exception { ODataServlet servlet = new ODataServlet(); prepareServlet(servlet); prepareRequest(reqMock, null, null); servlet.service(reqMock, respMock); Mockito.verify(respMock).setStatus(HttpStatusCodes.TEMPORARY_REDIRECT.getStatusCode()); Mockito.verify(respMock).setHeader(HttpHeaders.LOCATION, "/"); }
@Test public void serviceInstance() throws Exception { ODataServlet servlet = new ODataServlet(); prepareServlet(servlet); prepareRequest(reqMock, "", "/servlet-path"); Mockito.when(reqMock.getPathInfo()).thenReturn("/request-path-info"); Mockito.when(reqMock.getRequestURI()).thenReturn("http: ODataServiceFactory factory = Mockito.mock(ODataServiceFactory.class); ODataService service = Mockito.mock(ODataService.class); Mockito.when(factory.createService(Mockito.any(ODataContext.class))).thenReturn(service); ODataProcessor processor = Mockito.mock(ODataProcessor.class); Mockito.when(service.getProcessor()).thenReturn(processor); Mockito.when(reqMock.getAttribute(ODataServiceFactory.FACTORY_INSTANCE_LABEL)).thenReturn(factory); Mockito.when(respMock.getOutputStream()).thenReturn(Mockito.mock(ServletOutputStream.class)); servlet.service(reqMock, respMock); Mockito.verify(factory).createService(Mockito.any(ODataContext.class)); }