title: Setting up Spring-Data-Neo4j standalone application causes Failed to load ApplicationContext
body: <p>I have spent numerous hours of setting this up. I am trying to setup a JUnit to test a simple NodeEntity class and a Repository. I am also using Gradle (not Maven -- to be Gradle seems cleaner then the Maven pom.xml). Also, I am using the Java Configuration class. </p>
  
  <p><strong>build.gradle</strong>:</p>
  
  <pre><code>apply plugin:'java'
  apply plugin:'idea'
  
  project.ext {
      springVersion = "4.0.3.RELEASE"
      neo4jVersion = "2.0.1.RELEASE"
      springDataGraphVersion = "3.0.1.RELEASE"
  
      sourceCompatibility = 1.7
      version = '1.0'
  }
  
  configurations {
      runtime
      testCompile
  }
  repositories {
      mavenCentral()
      mavenLocal()
      maven {
          url "http://m2.neo4j.org/content/repositories/releases/"
      }
  }
  
  dependencies {
      compile 'org.slf4j:slf4j-api:1.7.5'
  
      compile "org.springframework:spring-context:${springVersion}"
      compile "org.neo4j:neo4j:${neo4jVersion}"
      // Provides Repository based Object &lt;-&gt; Graph Mapping
      compile "org.springframework.data:spring-data-neo4j:${springDataGraphVersion}"
      compile "javax.validation:validation-api:1.0.0.GA"
  
      testCompile 'junit:junit-dep:4.11'
      testCompile "org.springframework:spring-test:${springVersion}"
      testCompile 'org.hamcrest:hamcrest-all:1.3'
      // Access to Neo4j testing facilities: TestGraphDatabaseFactory
      testCompile "org.neo4j:neo4j-kernel:${neo4jVersion}:tests@jar"
      testRuntime 'org.slf4j:slf4j-simple:1.7.5'
  }
  
  // Generate wrapper for Gradle
  task wrapper(type: Wrapper) {
      gradleVersion = '1.11'
  }
  </code></pre>
  
  <p><strong>Test Class</strong></p>
  
  <pre><code>  import domains.BaseClass;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.neo4j.graphdb.GraphDatabaseService;
  import org.neo4j.graphdb.Transaction;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.transaction.annotation.Transactional;
  import repos.BaseClassRepository;
  
  import static org.junit.Assert.assertEquals;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes={AppConfigTest.class})
  public class TestBaseClass {
  
      @Autowired
      private BaseClassRepository baseClassRepository;
  
      @Autowired
      private GraphDatabaseService graphDb;
  
      @Transactional
      @Test
      public void shouldCreateNode () {
  
          BaseClass testClass = null;
  
          try (Transaction tx = graphDb.beginTx()) {
              testClass = new BaseClass();
              testClass.setName("Archer");
              baseClassRepository.save(testClass);
              tx.success();
          } catch (Exception e) {
              System.err.println(e.getMessage());
          }
  
  
          BaseClass testClass1 = baseClassRepository.findByName("Archer");
          assertEquals(testClass1.getName(), "Archer");
  
          System.out.println(testClass.getName());
      }
  }
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
      at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
      at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
      at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
      at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseClassRepository': Cannot resolve reference to bean 'neo4jTemplate' while setting bean property 'neo4jTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'neo4jTemplate' is defined
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:121)
      at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
      at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
      at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250)
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
      at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
      ... 44 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'neo4jTemplate' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
      ... 62 more
  </code></pre>
  
  <p>Also, for some reason the <code>shouldCreateNode()</code> function runs twice for the test. Not sure why either. =(</p>
  
  <p><strong>AppConfigTest Configuration</strong></p>
  
  <pre><code>import org.neo4j.graphdb.GraphDatabaseService;
  import org.neo4j.test.TestGraphDatabaseFactory;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
  import org.springframework.data.neo4j.config.Neo4jConfiguration;
  
  
  @Configuration
  @ComponentScan(basePackages = "domains")
  @EnableNeo4jRepositories(basePackages = "repos")
  public class AppConfigTest extends Neo4jConfiguration {
  
      public AppConfigTest () {
          setBasePackage("domains");
      }
  
      @Bean
      public GraphDatabaseService graphDatabaseService() {
          return new TestGraphDatabaseFactory().newImpermanentDatabase();
      }
  
  }
  </code></pre>