Spring Data Neo4j示例程序错误

时间:2012-12-08 11:26:12

标签: spring neo4j spring-data-neo4j

我正在创建一个小应用程序来了解Neo4j和Spring Data Neo4j。但我总是得到NullPointerException。因此,我使用了最大simple examples的Spring Data Neo4j之一,并使用JUnit中的main()更新了测试文件。我的测试文件如下:

package com.spring.nosql.neo4j.worldTest;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.graph.neo4j.template.Neo4jTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author srahul07
 * 
 */
@ContextConfiguration(locations = "classpath:neo4jBeans.xml")
@Transactional
public class Main {

    @Autowired
    private GalaxyService galaxyService;

    @Autowired
    private Neo4jTemplate template;

    @Rollback(false)
    @BeforeTransaction
    public void shouldAllowDirectWorldCreation() {
        // assertEquals(0, galaxyService.getNumberOfWorlds());
        System.out.println("Number of Worlds: "
                + galaxyService.getNumberOfWorlds());
        World myWorld = galaxyService.createWorld("mine", 0);
        System.out.println("Number of Worlds: "
                + galaxyService.getNumberOfWorlds());
        // assertEquals(1, galaxyService.getNumberOfWorlds());

        Iterable<World> foundWorlds = galaxyService.getAllWorlds();
        World mine = foundWorlds.iterator().next();
        System.out.println("Number of Worlds: " + myWorld.getName() + " Name: "
                + mine.getName());
        // assertEquals(myWorld.getName(), mine.getName());
    }

    public void shouldHaveCorrectNumberOfWorlds() {
        galaxyService.makeSomeWorlds();
        System.out.println("Number of Worlds: "
                + galaxyService.getNumberOfWorlds());
        // assertEquals(13, galaxyService.getNumberOfWorlds());
    }

    public void shouldFindWorldsById() {
        galaxyService.makeSomeWorlds();

        for (World world : galaxyService.getAllWorlds()) {
            World foundWorld = galaxyService.findWorldById(world.getId());
            System.out.println("Found World: " + foundWorld);
            // assertNotNull(foundWorld);
        }
    }

    public void shouldFindWorldsByName() {
        galaxyService.makeSomeWorlds();

        for (World world : galaxyService.getAllWorlds()) {
            World foundWorld = galaxyService.findWorldByName(world.getName());
            System.out.println("Found Worlds: " + foundWorld);
            // assertNotNull(foundWorld);
        }
    }

    public void shouldReachMarsFromEarth() {
        galaxyService.makeSomeWorlds();

        World earth = galaxyService.findWorldByName("Earth");
        World mars = galaxyService.findWorldByName("Mars");

        System.out.println("Mars Rechable from Earth: "
                + mars.canBeReachedFrom(earth));
        System.out.println("Earth Reachable from Mars: "
                + earth.canBeReachedFrom(mars));
        // assertTrue(mars.canBeReachedFrom(earth));
        // assertTrue(earth.canBeReachedFrom(mars));
    }

    public void shouldFindAllWorlds() {
        Collection<World> madeWorlds = galaxyService.makeSomeWorlds();
        Iterable<World> foundWorlds = galaxyService.getAllWorlds();

        int countOfFoundWorlds = 0;
        for (World foundWorld : foundWorlds) {
            System.out.println("Worlds Contains: "
                    + madeWorlds.contains(foundWorld));
            // assertTrue(madeWorlds.contains(foundWorld));
            countOfFoundWorlds++;
        }

        System.out.println("World's Size: " + madeWorlds.size()
                + " Count of Found Worlds: " + countOfFoundWorlds);
        // assertEquals(madeWorlds.size(), countOfFoundWorlds);
    }

    @SuppressWarnings("unchecked")
    public void shouldFindWorldsWith1Moon() {
        galaxyService.makeSomeWorlds();

        for (World worldWithOneMoon : galaxyService.findAllByNumberOfMoons(1)) {

            System.out.println("World With One Moon: "
                    + worldWithOneMoon.getName());
            System.out.println("World With One Moon is Earth?: "
                    + worldWithOneMoon.getName().contains("Earth"));
            System.out.println("World With One Moon is Earth?: "
                    + worldWithOneMoon.getName().contains("Midgard"));

            /*
             * assertThat( worldWithOneMoon.getName(),
             * is(anyOf(containsString("Earth"), containsString("Midgard"))));
             */
        }
    }

    public void shouldNotFindKrypton() {
        galaxyService.makeSomeWorlds();
        World krypton = galaxyService.findWorldByName("Krypton");
        System.out.println("World is Krypton: " + krypton.getName().isEmpty());
        // assertNull(krypton);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            Main mainTest = new Main();

            mainTest.shouldHaveCorrectNumberOfWorlds();

            mainTest.shouldAllowDirectWorldCreation();

            mainTest.shouldFindAllWorlds();

            mainTest.shouldFindWorldsById();

            mainTest.shouldFindWorldsByName();

            mainTest.shouldFindWorldsWith1Moon();

            mainTest.shouldNotFindKrypton();

            mainTest.shouldReachMarsFromEarth();

        } catch (Exception ae) {
            ae.printStackTrace();
        }

    }

}

我的Spring上下文配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/neo4j
        http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


    <context:spring-configured/>
    <context:annotation-config/>
    <context:component-scan base-package="com.spring.nosql.neo4j.worldTest" />

    <neo4j:config storeDirectory="target/neo4j-db"/>
    <neo4j:repositories base-package="com.spring.nosql.neo4j.worldTest"/>
</beans>

每当我运行测试代码时,都会出现以下错误:

java.lang.NullPointerException
    at com.spring.nosql.neo4j.worldTest.Main.shouldHaveCorrectNumberOfWorlds(Main.java:48)
    at com.spring.nosql.neo4j.worldTest.Main.main(Main.java:141)

我的配置中有什么错过的吗?或者这里有什么额外的需要吗?据我所知,Neo4j将在“target / neo4j-db”中创建图形数据库。另外,由于我的机器是Ubuntu,我在我的机器上安装了debian包Neo4j。所以我也试图通过使用REST API来使用它。所以为此,我对我的spring上下文文件进行了以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/neo4j
        http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


    <context:spring-configured/>
    <context:annotation-config/>
    <context:component-scan base-package="com.spring.nosql.neo4j.worldTest" />
<neo4j:repositories base-package="com.spring.nosql.neo4j.worldTest"/>

    <neo4j:config graphDatabaseService="graphDatabaseService"/> 
    <bean id="graphDatabaseService" 
      class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
      <constructor-arg index="0" value="http://localhost:7474/db/data" />
      <constructor-arg index="1" value="" />
      <constructor-arg index="2" value="" />
     </bean>

但我仍然得到同样的例外。

仅供参考:我已将Spring,Spring Data Neo4j和Neo4j库包含在Eclipse的buildpath中。

编辑:

好了,因为没有回复,我尝试了与JUnit Test案例相同的示例,如示例here中所示:

在我的JUnit测试用例中,我对每个方法都有以下错误:

java.lang.NoClassDefFoundError: org/neo4j/cypherdsl/grammar/Execute
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
    at java.lang.Class.getDeclaredMethods(Class.java:1808)
    at org.springframework.util.ReflectionUtils.findMethod(ReflectionUtils.java:154)
    at org.springframework.util.ClassUtils.getMostSpecificMethod(ClassUtils.java:730)
    at org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$AbstractFallbackTransactionAttributeSource.computeTransactionAttribute(TransactionalRepositoryProxyPostProcessor.java:326)
    at org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$AbstractFallbackTransactionAttributeSource.getTransactionAttribute(TransactionalRepositoryProxyPostProcessor.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy31.count(Unknown Source)
    at com.spring.nosql.neo4j.worldTest.GalaxyService.getNumberOfWorlds(GalaxyService.java:16)
    at com.spring.nosql.neo4j.worldTest.GalaxyServiceTest.shouldAllowDirectWorldCreation(GalaxyServiceTest.java:40)
    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:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    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:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.neo4j.cypherdsl.grammar.Execute
    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:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 43 more

这表示找不到或找不到名为:org / neo4j / cypherdsl / grammar / Execute的类。

这意味着我错过了其中一个库,但是Neo4j库显示了名为neo4j-cypher-1.8.jar的包。但在此我没有找到cypherdsl

EDIT2:

最后,它适用于JUnit,但不适用于public static void main() [psvm]。我错过了一个名为cypherdsl的库,可以从here下载。但是当我选择psvm时,为什么它不起作用?

0 个答案:

没有答案