java - 如何使用 boot 2.0 和 Neo4j SDN5 在 Spring 单元测试中配置我自己的 GraphDatabaseService 和 GraphAwareRuntime

标签 java neo4j spring-data spring-data-neo4j-4 graphaware

我正在编写一些单元测试,并希望将 TimeTree 与 Spring 存储库一起使用,以自动将事件节点附加到时间树。类似于 this问题,但我使用的是 boot 2.0 和 SDN5。我认为我的主要问题是我不知道如何设置配置,因此我的存储库和 TimeTree 使用相同的 GraphDatabaseService。我的@Confuration是这样的:

    @Configuration
    public class SpringConfig {

        @Bean
        public SessionFactory sessionFactory() {
            // with domain entity base package(s)
            return new SessionFactory(configuration(), "org.neo4j.boot.test.domain");
        }

        @Bean
        public org.neo4j.ogm.config.Configuration configuration() {
            return new org.neo4j.ogm.config.Configuration.Builder()
                .uri("bolt://localhost")
                .build();
        }

        @Bean
        public Session getSession() {
            return sessionFactory().openSession();
        }

        @Bean
        public GraphDatabaseService graphDatabaseService() {
            return new GraphDatabaseFactory()
                .newEmbeddedDatabase(new File("/tmp/graphDb"));
        }

        @Bean
        public GraphAwareRuntime graphAwareRuntime() {
            GraphDatabaseService graphDatabaseService = graphDatabaseService();
            GraphAwareRuntime runtime = GraphAwareRuntimeFactory
                .createRuntime(graphDatabaseService);

            runtime.registerModule(new TimeTreeModule("timetree",
                TimeTreeConfiguration
                    .defaultConfiguration()
                    .withAutoAttach(true)
                    .with(new NodeInclusionPolicy() {
                        @Override
                        public Iterable<Node> getAll(GraphDatabaseService graphDatabaseService) {
                            return null;
                        }

                        @Override
                        public boolean include(Node node) {
                            return node.hasLabel(Label.label("User"));
                        }
                    })
                    .withRelationshipType(RelationshipType.withName("CREATED_ON"))
                    .withTimeZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("GMT+1")))
                    .withTimestampProperty("createdOn")
                    .withResolution(Resolution.DAY)
    //                      .withCustomTimeTreeRootProperty("timeTreeName")
                    .withResolution(Resolution.HOUR), graphDatabaseService));
            runtime.start();
            return runtime;
        }
    }

我的测试如下所示:

    User user = new User("Michal");
    user.setCreatedOn(1431937636995l);
    userRepository.save(user);

    GraphUnit.assertSameGraph(graphDb, "CREATE (u:User {name:'Michal', createdOn:1431937636995})," +
            "(root:TimeTreeRoot)," +
            "(root)-[:FIRST]->(year:Year {value:2015})," +
            "(root)-[:CHILD]->(year)," +
            "(root)-[:LAST]->(year)," +
            "(year)-[:FIRST]->(month:Month {value:5})," +
            "(year)-[:CHILD]->(month)," +
            "(year)-[:LAST]->(month)," +
            "(month)-[:FIRST]->(day:Day {value:18})," +
            "(month)-[:CHILD]->(day)," +
            "(month)-[:LAST]->(day)," +
            "(day)<-[:CREATED_ON]-(u)"
    );

    GraphUnit.printGraph(graphDb);
    graphDb.shutdown();

有很多错误,但我认为它们都源于此:

Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [org.springframework.data.repository.support.Repositories]: 
Factory method 'repositories' threw exception; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'userRepository': Unsatisfied dependency 
expressed through method 'setSession' parameter 0; nested exception is 
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No 
qualifying bean of type 'org.neo4j.ogm.session.Session' available: 
expected single matching bean but found 2: getSession,
org.springframework.data.neo4j.transaction.SharedSessionCreator#0

最佳答案

这是因为配置类重新定义了一些Spring boot已经自动配置的bean(这里是Session)。

所以 Spring 注入(inject)不知道如何在两者之间进行选择。 删除 getSession() 应该会有所帮助。

第二件事是您的 SessionFactory 必须在 graphDatabaseService() 方法中使用嵌入式数据库设置。为此,请使用现有数据库配置嵌入式驱动程序。

应该适合您的摘要配置:

@Bean
public SessionFactory sessionFactory() {
    EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
    return new SessionFactory(driver, "org.neo4j.boot.test.domain");
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new Neo4jTransactionManager(sessionFactory());
}

@Bean
public GraphDatabaseService graphDatabaseService() {
    return new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase();
}

@Bean
public GraphAwareRuntime graphAwareRuntime() {
    ...

关于java - 如何使用 boot 2.0 和 Neo4j SDN5 在 Spring 单元测试中配置我自己的 GraphDatabaseService 和 GraphAwareRuntime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722136/

相关文章:

java - testng.xml 中的多个测试套件在 jar 内不起作用

neo4j - 密码查询 : Is it possible to "hide" an existing path with a "virtual relationship"?

neo4j - 为什么我必须 kill -9 neo4j?

java - spring 中不同事务管理器的嵌套事务

java - 是否可以拦截 spring-data-elasticsearch 中的保存或加载事件?

Java- AWT Applets- 如何更改程序中形状的大小?

javascript - 即使对象已正确实例化,JSONObject 也无法从现有对象中读取属性

Java Apache POI - 获取单元格匹配正则表达式右侧单元格的值?

neo4j - 在 Neo4j 中将实体与适当的关系相关联?

spring-data - 如何正确使用 Locking 或 Transactions 来防止使用 Spring Data 重复