### Question:
DiagnosticContextFilter implements Filter { @Override public void init(FilterConfig config) throws ServletException { String headerName = config.getInitParameter(CORRELATION_ID_HEADER_NAME_PARAM); if (headerName == null) { LOG.debug("Parameter {} not configured via filter config.", CORRELATION_ID_HEADER_NAME_PARAM); } else { this.correlationIdHttpHeaderName = headerName; } LOG.info("Correlation ID header initialized to: {}", this.correlationIdHttpHeaderName); if (this.diagnosticContextFacade == null) { try { ServletContext servletContext = config.getServletContext(); org.springframework.web.context.WebApplicationContext springContext; springContext = org.springframework.web.context.support.WebApplicationContextUtils .getWebApplicationContext(servletContext); this.diagnosticContextFacade = springContext.getBean(DiagnosticContextFacade.class); } catch (Throwable e) { LOG.warn("DiagnosticContextFacade not defined in spring. Falling back to default", e); this.diagnosticContextFacade = new DiagnosticContextFacadeImpl(); } } } DiagnosticContextFilter(); void setCorrelationIdHttpHeaderName(String correlationIdHttpHeaderName); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Inject void setDiagnosticContextFacade(DiagnosticContextFacade diagnosticContextFacade); @Override void init(FilterConfig config); static final String CORRELATION_ID_HEADER_NAME_DEFAULT; }### Answer:
@Test public void testInitWithNullInitParameter() throws Exception { DiagnosticContextFilter filter = new DiagnosticContextFilter(); String field = (String) ReflectionTestUtils.getField(DiagnosticContextFilter.class, CORRELATION_ID_HEADER_NAME_PARAM_FIELD_NAME); assertThat(field).isNotNull(); when(this.config.getInitParameter(field)).thenReturn(null); filter.init(this.config); String correlationIdHttpHeaderName = (String) ReflectionTestUtils.getField(filter, CORRELATION_ID_HEADER_NAME_PARAM); assertThat(correlationIdHttpHeaderName).isNotNull() .isEqualTo(DiagnosticContextFilter.CORRELATION_ID_HEADER_NAME_DEFAULT); }
@Test public void testInitWithNonDefaultParameter() throws Exception { DiagnosticContextFilter filter = new DiagnosticContextFilter(); String field = (String) ReflectionTestUtils.getField(DiagnosticContextFilter.class, CORRELATION_ID_HEADER_NAME_PARAM_FIELD_NAME); assertThat(field).isNotNull(); String nonDefaultParameter = "test"; when(this.config.getInitParameter(field)).thenReturn(nonDefaultParameter); filter.init(this.config); String correlationIdHttpHeaderName = (String) ReflectionTestUtils.getField(filter, CORRELATION_ID_HEADER_NAME_PARAM); assertThat(correlationIdHttpHeaderName).isEqualTo(nonDefaultParameter); }