title: Spring using MockMvc Test with CORS filter
body: <p>I have am trying to run a basic MVC test</p>
  
  <pre><code>@Test
  public void shouldReturnDefaultMessage() throws Exception {
      this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
              .andExpect(content().string(containsString("Hello World")));
  }
  </code></pre>
  
  <p>However, this will always result in <code>java.lang.IllegalArgumentException: Header value must not be null</code>
  I found out that <strong>if I deactivate the CORS filter the test will work</strong> without errors.</p>
  
  <p>My SimpleCORSFilter</p>
  
  <pre><code>@Component
  public class SimpleCORSFilter implements Filter {
  
      private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);
  
      public SimpleCORSFilter() {
          log.info("SimpleCORSFilter init");
      }
  
      @Override
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
              throws IOException, ServletException {
  
          HttpServletRequest request = (HttpServletRequest) req;
          HttpServletResponse response = (HttpServletResponse) res;
  
          response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
          response.setHeader("Access-Control-Allow-Credentials", "true");
          //...
          chain.doFilter(req, res);
      }
  
  }
  </code></pre>
  
  <p>Part of my Security Config</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      UserDetailsServiceImp userDetailsService;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable().authorizeRequests()
                  .antMatchers("/").permitAll()
                  .anyRequest().authenticated()
                  .and()
                  .addFilterBefore(new SimpleCORSFilter(),UsernamePasswordAuthenticationFilter.class);
      }
  }
  </code></pre>
  
  <p>Only if I remove the @Component in the SimpleCORSFilter and remove the line <code>.addFilterBefore(new SimpleCORS...)</code> in SecurityConfig the test works.</p>
  
  <p>How can I use mockMVC in my test? Either how do I disable the CORSFilter for the test or how do I make the request in mockMvc correctly so it doesn't throw an error about "header value must not be null".</p>
  
  <p>I have tried setting a random header value in the mockMvc but that didn't change the error.</p>