### Question:
TraceeInterceptor implements HandlerInterceptor { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object o) { final TraceeFilterConfiguration configuration = backend.getConfiguration(profileName); if (configuration.shouldProcessContext(IncomingRequest)) { @SuppressWarnings("unchecked") final Enumeration<String> headers = request.getHeaders(incomingHeaderName); if (headers != null && headers.hasMoreElements()) { final Map<String, String> parsedContext = httpHeaderSerialization.parse(Collections.list(headers)); backend.putAll(configuration.filterDeniedParams(parsedContext, IncomingResponse)); } } Utilities.generateInvocationIdIfNecessary(backend); final HttpSession session = request.getSession(false); if (session != null) { Utilities.generateSessionIdIfNecessary(backend, session.getId()); } writeHeaderIfUncommitted(response); return true; } 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 shouldSetInvocationIdToBackend() throws Exception { unit.preHandle(httpServletRequest, httpServletResponse, new Object()); verify(mockedBackend).put(eq(INVOCATION_ID_KEY), anyString()); }
@Test public void shouldSetSessionIdToBackendHttpSessionExist() throws Exception { when(httpServletRequest.getSession(anyBoolean())).thenReturn(httpServletSession); when(httpServletSession.getId()).thenReturn(anyString()); unit.preHandle(httpServletRequest, httpServletResponse, new Object()); verify(mockedBackend).put(eq(TraceeConstants.SESSION_ID_KEY), anyString()); }
@Test public void shouldNotOverrideExistingInvocationId() throws Exception { when(mockedBackend.containsKey(eq(INVOCATION_ID_KEY))).thenReturn(true); unit.preHandle(httpServletRequest, httpServletResponse, new Object()); verify(mockedBackend, never()).put(eq(INVOCATION_ID_KEY), anyString()); }
@Test public void shouldNotOverrideExistingSessionId() throws Exception { when(mockedBackend.containsKey(eq(TraceeConstants.SESSION_ID_KEY))).thenReturn(true); unit.preHandle(httpServletRequest, httpServletResponse, new Object()); verify(mockedBackend, never()).put(eq(TraceeConstants.SESSION_ID_KEY), anyString()); }
@Test public void shouldWriteInitTpicToResponse() { final HashMap<String, String> parsedInitValues = new HashMap<>(); parsedInitValues.put(INVOCATION_ID_KEY, "myGreatId321"); when(mockedBackend.copyToMap()).thenReturn(parsedInitValues); when(httpServletResponse.isCommitted()).thenReturn(false); unit.preHandle(httpServletRequest, httpServletResponse, new Object()); verify(httpServletResponse).setHeader(eq(TraceeConstants.TPIC_HEADER), contains(INVOCATION_ID_KEY + "=myGreatId321")); }
@Test public void shouldMergeIncomingContextIfConfigured() throws Exception { final Map<String, String> expected = new HashMap<>(); expected.put("testkey", "testValue123"); final Enumeration<String> header = createHeader("testkey", "testValue123"); when(httpServletRequest.getHeaders(TraceeConstants.TPIC_HEADER)).thenReturn(header); unit.preHandle(httpServletRequest, httpServletResponse, new Object()); verify(mockedBackend).putAll(eq(expected)); }