MDCClearFilter extends OncePerRequestFilter { @Override protected final void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { try { filterChain.doFilter(request, response); } finally { MDC.clear(); } }    }
@Test public void testDoFilterInternal_normal() throws ServletException, IOException { MDC.put("key0", "value0"); testTarget.doFilterInternal(mockRequest, mockResponse, mockFilterChain); assertThat(mockFilterChain.actualMdcContextMap.size(), is(1)); assertThat(mockFilterChain.actualMdcContextMap.containsKey("key0"), is( true)); assertThat(MDC.getCopyOfContextMap(), is(nullValue())); }
@Test public void testDoFilterInternal_occur_exception() throws ServletException, IOException { ServletException occurException = new ServletException("test"); doThrow(occurException).when(mockFilterChain).doFilter(mockRequest, mockResponse); MDC.put("key0", "value0"); try { testTarget.doFilterInternal(mockRequest, mockResponse, mockFilterChain); fail("don't occur ServletException."); } catch (ServletException e) { assertThat(e, is(occurException)); } assertThat(MDC.getCopyOfContextMap(), is(nullValue())); }