title: Why removing WebMvcTest annotation from my test in SpringBoot application causes ComponentScan to fail?
body: <p>I have a test defined with:</p>
  
  <pre><code>@ComponentScan(basePackages = { ... })
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = { AppConfig.class })
  @WebMvcTest(secure = false)
  public class MyTest extends AbstractJUnit4SpringContextTests  {
  
       @Autowired SomeClass target;
  
       @Test public void test() { Assert.assertTrue(target != null); } // MCVE
  
  }
  </code></pre>
  
  <p>Here's my ContextConfiguration class:</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Bean
      @ConfigurationProperties(prefix = "datasource")
      public DataSource dataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      public JdbcTemplate jdbcTemplate(DataSource dataSource) {
          return new JdbcTemplate(dataSource);
      }
  
      @Bean
      ServletWebServerFactory servletWebServerFactory() {
          return new TomcatServletWebServerFactory();
      }
  
  }
  </code></pre>
  
  <p>This test works fine, but it wastes a lot of time in MVC initialization due to the <code>@WebMvcTest</code> annotation. This particular test does not require any MVC functionality. The annotation ended up in there as a result of trial-and-error, to get the <code>@Autowired</code> annotation to work correctly. But now I want to remove it.</p>
  
  <p>So... I went ahead and removed the <code>@WebMvcTest</code> annotation. But then the test fails because apparently the <code>@ComponentScan</code> does not have any effect unless <code>@WebMvcTest</code> is present (which I now remember, is the reason I added it).</p>
  
  <p>Here's the actual error message:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '...' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>
  
  <p>I tried adding <code>@SpringBootTest</code> but it made no difference.</p>
  
  <p>If instead, I add <code>@EnableAutoConfiguration</code>, I get a different error:</p>
  
  <pre><code>java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
  </code></pre>
  
  <p>What annotation am I missing?</p>