title: springSecurityFilterChain bean not found using MockMVC and @ComponentScan
body: <p>I'm testing a spring rest controller using spring MockMVC.
  I am able to execute mock http requests and verify the response.
  My setup autowires the WebApplicationContext: </p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebAppConfiguration
  @ContextConfiguration
  public class MyTestClass {
  
  @Autowired
  private WebApplicationContext webAppContext;
  
  private MockMvc mockMvc;
  
  @Before
  public void setup() {
      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webAppContext)
              .apply(springSecurity()).build();
  }
  </code></pre>
  
  <p>To tell MockMVC what controllers to initialize and add to the context I have an internal config class:</p>
  
  <pre><code>@Configuration
  @ComponentScan(
          basePackages = {"com.acme"},
          useDefaultFilters = false,
          includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value={MyRestController.class, MyRestControllerDependency.class})
          })
  @EnableWebMvc
  static class MyTestClassConfig implements WebMvcConfigurer {
  
      @Bean
      public MyClassRepository myClassRepository() {
          return Mockito.mock(MyClassRepository.class);
      }
  </code></pre>
  
  <p>And this works like a charm. MockMVC spins up a container with a context that loaded MyRestController and serves the urls mapped by MyRestController.
  Now, some of MyRestControllers methods are annotated with: <code>@PreAuthorize("hasAuthority('ADMINISTRATOR')")</code> so the next step is to test that.</p>
  
  <p>Reading the Integration part of the Spring security testing guide
  <a href="https://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html#test-mockmvc-setup" rel="nofollow noreferrer">spring security and test-mockmvc</a>
   it seems all I should do is call the org.springframework.security.test.web.servlet.setup.springSecurity() in the MockMvcBuilders <code>.apply()</code> function calls. </p>
  
  <p>This does not work, I get:</p>
  
  <pre><code>java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
  </code></pre>
  
  <p>I tried adding a bean definition to my internal config class like so:</p>
  
  <pre><code>    Filter springSecurityFilterChain() {
          return new FilterChainProxy();
      }
  </code></pre>
  
  <p>While this does provide a springSecurityFilterChain, the instance is empty and when I run the test I get a NPE:</p>
  
  <pre><code>java.lang.NullPointerException
  at org.springframework.security.web.FilterChainProxy.getFilters(FilterChainProxy.java:224)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  </code></pre>
  
  <p>Which, is understandable, by instantiating the FilterChainProxy directly, it is indeed empty.</p>
  
  <p>I suspect I need to configure my component scan to include springs' instances of the FilterChainProxy. <em>If</em> that is the issue, I don't know what classes I should include. I have tried including all the classes:</p>
  
  <pre><code>        includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value={TenantAPI.class, TenantManager.class}),
                            @ComponentScan.Filter(type = FilterType.REGEX, pattern="^org\\.springframework\\.security.*")})
  </code></pre>
  
  <p>But this gives the same error: "Ensure a Bean with the name springSecurity.." as when I run the test without the additional Regex filter. </p>
  
  <p>Does anyone know how I can get the springSecurityFilterChain when I am being picky with the internal classes I let MockMVC instantiate?  </p>