### Question:
LoggingFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { try { String requestID = String.valueOf(UUID.randomUUID().getMostSignificantBits()); LOGGER.debug(HEADER_REQUEST_ID + ": " + requestID); MDC.put(HEADER_REQUEST_ID, requestID); if (req instanceof HttpServletRequest) { HttpServletRequest httpReq = ((HttpServletRequest) req); httpReq.setAttribute(HEADER_REQUEST_ID, requestID); if (httpReq.getUserPrincipal() != null) { String principal = httpReq.getUserPrincipal().getName(); if (principal != null) { MDC.put(HEADER_REQUEST_PRINCIPAL, principal); } } } else { LOGGER.info("ServletRequest of type: " + req.getClass()); } if (resp instanceof HttpServletResponse) { ((HttpServletResponse) resp).setHeader(HEADER_REQUEST_ID, requestID); } else { LOGGER.info("ServletResponse of type: " + resp.getClass()); } LOGGER.debug("log for testing"); chain.doFilter(req, resp); } finally { MDC.remove(HEADER_REQUEST_ID); MDC.remove(HEADER_REQUEST_PRINCIPAL); } }  @Override void destroy(); @Override void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain); @Override void init(FilterConfig arg0); static final String HEADER_REQUEST_ID; static final String HEADER_REQUEST_PRINCIPAL; }### Answer:
@Test public void testRequest_SetAttribute_RequestID() throws Exception { ArgumentCaptor<String> captureID = ArgumentCaptor.forClass(String.class); new LoggingFilter().doFilter(request, response, chain); new LoggingFilter().doFilter(request, response, chain); verify(request, times(2)).setAttribute(eq(LoggingFilter.HEADER_REQUEST_ID), captureID.capture()); List<String> values = captureID.getAllValues(); assertNotNull(values); assertEquals(2, values.size()); assertNotNull(values.get(0)); assertNotNull(values.get(1)); assertNotEquals(values.get(0), values.get(1)); }
@Test public void testResponse_SetHeader_RequestID() throws Exception { ArgumentCaptor<String> captureID = ArgumentCaptor.forClass(String.class); new LoggingFilter().doFilter(request, response, chain); new LoggingFilter().doFilter(request, response, chain); verify(response, times(2)).setHeader(eq(LoggingFilter.HEADER_REQUEST_ID), captureID.capture()); List<String> values = captureID.getAllValues(); assertNotNull(values); assertEquals(2, values.size()); assertNotNull(values.get(0)); assertNotNull(values.get(1)); assertNotEquals(values.get(0), values.get(1)); }