2017-10-13 39 views
1

我正在編寫一些單元測試,希望將TimeTree與Spring存儲庫一起使用,以將事件節點自動附加到時間樹。像this問題,但我使用引導2.0和SDN5。我認爲我的主要問題是我不知道如何設置配置,所以我的存儲庫和我的TimeTree使用相同的GraphDatabaseService。我@Confuration是這樣的:如何在啓動2.0和Neo4j的彈簧單元測試中配置我自己的GraphDatabaseService和GraphAwareRuntime SDN5

@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 

回答

1

這是因爲配置類重新定義了一些由Spring啓動自動配置的bean(這裏是Session)。

所以彈簧注射不知道如何選擇2. 刪除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() { 
    ... 
+1

非常感謝,貌似我回來的業務。如果我們碰巧遇到啤酒,就喝啤酒吧! –