title: @Autowired dependency injection not occurring in junit test
body: <p>I'm using Spring Boot to bootstrap a spring-data-neo4j application. My unit tests (with no dependencies injected) run fine, but when I try to run an integration test with an <code>@Autowired</code> dependency on a <code>@Service</code>-annotated class, it's failing on a <code>NoSuchBeanDefinitionException</code>. </p>
  
  <p>It seems like the context isn't being loaded in the unit test for some reason, but I've annotated the test with <code>@SpringApplicationConfiguration(classes = AppConfig.class)</code> - is there something else I need to do here? </p>
  
  <p><strong>Configuration class</strong></p>
  
  <pre><code>@Configuration
  @EnableAutoConfiguration
  @ComponentScan(basePackages = "net.foo.bar")
  @EnableNeo4jRepositories(basePackages = "net.foo.bar.repo")
  public class AppConfig extends Neo4jConfiguration {
  
      public AppConfig() {
          setBasePackage("net.foo.bar");
      }
  
      @Bean(destroyMethod = "shutdown")
      @Scope(BeanDefinition.SCOPE_SINGLETON)
      public GraphDatabaseService graphDatabaseService() {
          return new GraphDatabaseFactory().newEmbeddedDatabase(filePath);
      }
  
      public static void main(String[] args) {
          ApplicationContext ctx = SpringApplication.run(AppConfig.class, args);
      }
  }
  </code></pre>
  
  <p><strong>Service class:</strong> </p>
  
  <pre><code>package net.foo.bar.core.service
  @Service
  public class PostService implements EntityService&lt;PostDAO,Post&gt; {
  
      @Autowired
      Neo4jTemplate template;
      //...
      //don't think anything else here is relevant
  }
  </code></pre>
  
  <p><strong>Test class:</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = AppConfig.class)
  public class PostTests {
      @Autowired
      PostService postService;
  
      @Test
      public void someTest(){ 
          postService.doSomething();
          //...
      }
  
   }
  </code></pre>
  
  <p><strong>Stack trace:</strong></p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'net.foo.bar.PostTests': ....
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.foo.bar.core.service.PostService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
  ... 31 common frames omitted
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>As a workaround, rather than autowiring my service directly, I tried autowiring a reference to the <code>ApplicationContext</code> and instantiating my service through a call to <code>getBeanOfType()</code> in my <code>setUp()</code> method: </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Epublic.class)
  public class PostTests {
      @Autowired
      ApplicationContext ctx;
  
      PostService service; 
  
      @Before
      public void setUp() {
          service = ctx.getBean("postServiceImpl", PostService.class);
      }
  }
  </code></pre>
  
  <p>This is working, but I feel like I'm hitting the target but missing the point here...</p>