title: Spring boot test fails with No qualifying bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath'
body: <p>I have a Spring Boot application which works when run, but I am getting an error when I try to do some testing with MockMVC. The error is triggered by one line in the SecurityConfig:</p>
  
  <pre><code>val publicUrls = arrayOf(
      "/telemetry/**",
      "/login",
      "/logout"
  )
  
  override fun configure(http: HttpSecurity) {
      http
          .csrf().ignoringAntMatchers(*publicUrls)
          .and()
          .authorizeRequests()
          .requestMatchers(EndpointRequest.to("status", "info")).permitAll()
  
          // The following line is the problem
          .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()   // &lt;-- causes error
  
          .antMatchers(*publicUrls).permitAll()
          .antMatchers("/system/**").hasRole("SUPER")
          .antMatchers("/admin/**").hasRole("ADMIN")
          .anyRequest().authenticated()
      //.and()
      //.oauth2Login();
  }
  </code></pre>
  
  <p>Without the line allowing access to static resources, the test works correctly. With this line in place, I get the error below (with stack trace). If I add a class implementing <code>DispatcherServletPath</code> annotated with <code>@Component</code> then I get an error <code>expected single matching bean but found 2:</code> instead.</p>
  
  <p>The test code is:</p>
  
  <pre><code>@SpringBootTest
  @AutoConfigureMockMvc
  @WebAppConfiguration
  @ComponentScan("com.controlj.monitor.service")
  @RunWith(SpringJUnit4ClassRunner::class)
  class SampleTest {
  
      @MockBean
      private lateinit var mockTelemetryService: TelemetryService
      @Autowired
      private lateinit var springSecurityFilterChain: FilterChainProxy
  
      private lateinit var mvc: MockMvc
  
      @Before
      fun setup() {
          mvc = MockMvcBuilders.standaloneSetup(TelemetryEndpoint(mockTelemetryService))
              .apply&lt;StandaloneMockMvcBuilder&gt;(SecurityMockMvcConfigurers.springSecurity(springSecurityFilterChain))
              .build()
      }
  
     @Test
      fun testTelemetryApi() {
          val postMapping = TelemetryEndpoint::post.findAnnotation&lt;PostMapping&gt;()?.value?.first()
          mvc.perform(post(postMapping, "badKey", 123456)
              .contentType(MediaType.APPLICATION_JSON))
              .andExpect(status().`is`(404))
     }
  }
  </code></pre>
  
  <p>How do I get around this?</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' available
  
  at org.springframework.beans.factory.support.StaticListableBeanFactory.getBean(StaticListableBeanFactory.java:172)
  at org.springframework.test.web.servlet.setup.StubWebApplicationContext.getBean(StubWebApplicationContext.java:176)
  at org.springframework.boot.security.servlet.ApplicationContextRequestMatcher.lambda$createContext$1(ApplicationContextRequestMatcher.java:95)
  at org.springframework.boot.autoconfigure.security.servlet.StaticResourceRequest$StaticResourceRequestMatcher.initialized(StaticResourceRequest.java:140)
  at org.springframework.boot.security.servlet.ApplicationContextRequestMatcher.getContext(ApplicationContextRequestMatcher.java:73)
  at org.springframework.boot.security.servlet.ApplicationContextRequestMatcher.matches(ApplicationContextRequestMatcher.java:57)
  at org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource.getAttributes(DefaultFilterInvocationSecurityMetadataSource.java:95)
  at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:197)
  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:124)
  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
  at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
  at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
  at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:182)
  </code></pre>