Spring Data Neo4j 4.0.0.M1 Test Configuration

2019-08-09 18:17发布

问题:

I am very new to Spring Data and I am trying to configure my Spring Data Neo4j 4.0.0.M1 Test. It has taken me 2 days without any result and I can't seem to find any example on SDN4 to do this.

Here is my simple test class :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyConfigurationTest.class)
@DirtiesContext
public class ProjectRepositoryTests {

    @Autowired
    PlatformTransactionManager transactionManager;

    @Autowired
    ProjectRepository projectRepository;

    @Transactional
    @Test
    public void testAddProject() {
        Project project = new Project();
        project.setName("ProjectA");
        project.setCreationDate(new Date());
        project.setDescription("Description");
        projectRepository.save(project);
        Project addedProject = projectRepository.findProjectByName("ProjectA");
        Assert.assertNotNull("should have found something", addedProject);
        projectRepository.delete(addedProject);
    }
}

Here is my configuration class :

@Configuration
@EnableNeo4jRepositories(basePackages = "project.repository")
@EnableTransactionManagement
public class MyConfigurationTest extends Neo4jConfiguration {

    @Bean
    public Neo4jServer neo4jServer() {
        return new InProcessServer();
    }

    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }

    @Bean
    public SessionFactory getSessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory("project.domain");
    }
}

and here is my dependencies :

dependencies {
    compile "org.springframework.data:spring-data-neo4j:${SDNVersion}"
    compile "com.rabbitmq:amqp-client:${RMQVersion}"
    compile "com.google.code.gson:gson:${GSONVersion}"
    compile "org.neo4j:neo4j:${Neo4jVersion}"
    compile "com.graphaware.neo4j:timetree:${Neo4jTimeTreeVersion}"

    testCompile group: 'org.neo4j', name: 'neo4j-ogm-test', version: '1.0.0.BUILD-SNAPSHOT', classifier: 'tests'
    testCompile group: 'org.springframework.data', name: 'spring-data-neo4j', version: SDNVersion, classifier: 'tests'
    testCompile group: 'org.springframework', name: 'spring-test', version: SpringTestVersion
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

The problem is, I got this error :

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jServer' defined in class MyConfigurationTest: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.server.Neo4jServer MyConfigurationTest.neo4jServer()] threw exception; nested exception is java.lang.NoClassDefFoundError: org/neo4j/server/helpers/CommunityServerBuilder
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
    ... 28 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.neo4j.server.Neo4jServer MyConfigurationTest.neo4jServer()] threw exception; nested exception is java.lang.NoClassDefFoundError: org/neo4j/server/helpers/CommunityServerBuilder
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    ... 45 more
Caused by: java.lang.NoClassDefFoundError: org/neo4j/server/helpers/CommunityServerBuilder
    at org.springframework.data.neo4j.server.InProcessServer.<init>(InProcessServer.java:31)
    at MyConfigurationTest.neo4jServer(MyConfigurationTest.java:23)
    at MyConfigurationTest$$EnhancerBySpringCGLIB$$44afa770.CGLIB$neo4jServer$0(<generated>)
    at MyConfigurationTest$$EnhancerBySpringCGLIB$$44afa770$$FastClassBySpringCGLIB$$d8d802f1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:311)
    at MyConfigurationTest$$EnhancerBySpringCGLIB$$44afa770.neo4jServer(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 46 more
Caused by: java.lang.ClassNotFoundException: org.neo4j.server.helpers.CommunityServerBuilder
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 58 more

Thank you in advance and any help would be really appreciated!

回答1:

For Maven, please include the following dependency:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.0.0.M1</version>
        <type>test-jar</type>
    </dependency>

For Gradle this should work:

    dependencies {
       compile(group: 'org.springframework.data', 
          name: 'spring-data-neo4j', 
          version: '4.0.0.M1', 
          classifier: 'tests')
    } 

If you want the latest green build version, it is available as '4.0.0-BUILD-SNAPSHOT'. This version has a number of bug fixes and additional features since M1.

To use the BUILD-SNAPSHOT release you'll need to add this repository:

Maven:

    <repository>
        <id>spring-libs-snapshot</id>
        <url>http://repo.spring.io/libs-snapshot</url>
    </repository>

Gradle:

    repositories {
       spring-libs-snapshot {
          url 'http://repo.spring.io/libs-snapshot'
       }
    }  


回答2:

Looks like you need these dependencies as well:

        <dependency>
            <groupId>org.neo4j.app</groupId>
            <artifactId>neo4j-server</artifactId>
            <version>${neo4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-kernel</artifactId>
            <version>${neo4j.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-io</artifactId>
            <version>${neo4j.version}</version>
            <type>test-jar</type>
        </dependency>

Edit: add this one as well

 <dependency>
        <groupId>org.neo4j.app</groupId>
        <artifactId>neo4j-server</artifactId>
        <version>${neo4j.version}</version>
        <type>test-jar</type>
    </dependency>

Will check if this is intended or not, but for now, please add these and see if you can proceed.