### Question:
ToolkitHttpServlet extends HttpServlet { protected final void outputException(final Throwable e, final HttpServletRequest req, final HttpServletResponse resp, final int httpStatusCode) { if (httpStatusCode >= 0) { resp.setStatus(httpStatusCode); } resp.setContentType("application/json;charset=UTF-8"); try { final PrintWriter output = resp.getWriter(); if(e instanceof APIException) { setLocalization((APIException) e, LocaleUtils.getUserLocaleAsString(req)); } output.print(e == null ? "" : JSonSerializer.serialize(e)); output.flush(); } catch (final Exception e2) { throw new APIException(e2); } } ToolkitHttpServlet(); @Override final void service(final ServletRequest req, final ServletResponse res);  }### Answer:
@Test public void testOutputExceptionPrintProperJson() throws Exception { APIException exception = new APIException("message"); doReturn(writer).when(resp).getWriter(); toolkitHttpServlet.outputException(exception, req, resp, 500); verify(writer).print(exception.toJson()); }
@Test public void testLocaleIsPassedFromRequestToException() throws Exception { APIException exception = mock(APIException.class, withSettings().defaultAnswer(RETURNS_MOCKS)); doReturn(writer).when(resp).getWriter(); doReturn(new Cookie[] { new Cookie(LocaleUtils.LOCALE_COOKIE_NAME, "fr_FR") }).when(req).getCookies(); toolkitHttpServlet.outputException(exception, req, resp, 500); verify(exception).setLocale(LOCALE.fr); }
@Test public void testIfLocaleIsNotInACookieThatBrowserLocaleIsPassedThrough() throws Exception { APIException exception = mock(APIException.class, withSettings().defaultAnswer(RETURNS_MOCKS)); doReturn(writer).when(resp).getWriter(); doReturn(new Cookie[0]).when(req).getCookies(); doReturn(Locale.CANADA_FRENCH).when(req).getLocale(); toolkitHttpServlet.outputException(exception, req, resp, 500); verify(exception).setLocale(LOCALE.fr); }
@Test public void testIfLocaleIsNotInACookieNorBrowserThatDefaultLocaleIsPassedThrough() throws Exception { APIException exception = mock(APIException.class, withSettings().defaultAnswer(RETURNS_MOCKS)); doReturn(writer).when(resp).getWriter(); doReturn(new Cookie[0]).when(req).getCookies(); doReturn(null).when(req).getLocale(); toolkitHttpServlet.outputException(exception, req, resp, 500); verify(exception).setLocale(LOCALE.en); }