本文介绍了如何在带有启动2.0和Neo4j SDN5的弹簧单元测试中配置自己的GraphDatabaseService和GraphAwareRuntime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing some unit tests and want to use TimeTree along with Spring repositories, to auto attach event nodes to a time tree. Something like this issue, but I'm using boot 2.0 and SDN5. I think my main issue is I don't know how to set up the configuration so my repositories and my TimeTree use the same GraphDatabaseService. My @Confuration is like this:

    @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();

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

There's a host of errors, but I think they all stem from this one:

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).

It is because the configuration class redefines some beans already automatically configured by Spring boot (here the Session).

因此弹簧注入不知道如何在2之间进行选择.删除getSession()应该会有所帮助.

So spring injection does not know how to choose between the 2.Removing the getSession() should help.

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

A second thing is that your SessionFactory has to use the embedded DB setup in the graphDatabaseService() method. For this, configure an embedded driver with the existing database.

适合您的概要配置:

@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() {
    ...

这篇关于如何在带有启动2.0和Neo4j SDN5的弹簧单元测试中配置自己的GraphDatabaseService和GraphAwareRuntime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:40