### Question:
TraceeInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) { try { writeHeaderIfUncommitted(response); } finally { backend.clear(); } } TraceeInterceptor();  TraceeInterceptor(TraceeBackend backend); @Override boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object o); @Override void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView); @Override void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e); void setOutgoingHeaderName(String outgoingHeaderName); void setIncomingHeaderName(String incomingHeaderName); String getProfileName(); void setProfileName(String profileName);  }### Answer:
@Test public void doNotWriteHeadersToResponseIfResponseIsCommited() { final HashMap<String, String> parsedInitValues = new HashMap<>(); parsedInitValues.put(INVOCATION_ID_KEY, "myGreatId321"); when(mockedBackend.copyToMap()).thenReturn(parsedInitValues); when(httpServletResponse.isCommitted()).thenReturn(true); unit.afterCompletion(httpServletRequest, httpServletResponse, new Object(), null); verify(httpServletResponse, times(0)).setHeader(eq(TraceeConstants.TPIC_HEADER), Mockito.anyString()); }
@Test public void shouldRenderContextInResponseIfConfigured() throws Exception { mockedBackend.put(INVOCATION_ID_KEY, "123"); unit.afterCompletion(httpServletRequest, httpServletResponse, new Object(), null); verify(httpServletResponse).setHeader(eq(TraceeConstants.TPIC_HEADER), anyString()); }
@Test public void shouldCleanupAfterProcessing() throws Exception { unit.afterCompletion(httpServletRequest, httpServletResponse, new Object(), null); verify(mockedBackend).clear(); }