Neo4j:正确单元测试螺栓驱动程序 [英] Neo4j: unit testing the bolt driver properly

查看:25
本文介绍了Neo4j:正确单元测试螺栓驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 http://neo4j.com/developer/将 Neo4j Bolt 驱动程序添加到我的应用程序中java/:

import org.neo4j.driver.v1.*;

Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );

Session session = driver.session();
session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );

StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );

while ( result.hasNext() )

{
    Record record = result.next();
    System.out.println( record.get( "title" ).asString() + " " + record.get("name").asString() );
}
session.close();
driver.close();

然而,总是来自官方文档单元测试使用:

However, always from the official documentation unit testing is made using:

GraphDatabaseService db = new TestGraphDatabaseFactory()
            .newImpermanentDatabaseBuilder()

所以如果我想以某种方式测试上面的代码,我必须用 GraphDatabaseService 替换 GraphDatabase.driver("bolt://localhost",...) 来自测试.我怎样才能做到这一点?据我所知,我无法从那里提取任何类型的 内存驱动程序.

So if I want to test in some way the code above, I have to replace the GraphDatabase.driver( "bolt://localhost",...) with the GraphDatabaseService from the test. How can I do that? I cannot extract any sort of in-memory driver from there as far as I can see.

推荐答案

Neo4j JDBC 有一个名为 Neo4jBoltRule 用于单元测试.这是一个junit规则启动/停止一个临时数据库以及一些启动bolt的配置.

The Neo4j JDBC has a class called Neo4jBoltRule for unit testing. It is a junit rule starting/stopping an impermanent database together with some configuration to start bolt.

规则类使用动态端口分配来防止由于并行运行多个测试而导致的测试失败(想想您的 CI 基础架构).

The rule class uses dynamic port assignment to prevent test failure due to running multiple tests in parallel (think of your CI infrastructure).

使用该规则类的单元测试示例可在 https://github.com/neo4j-contrib/neo4j-jdbc/blob/master/neo4j-jdbc-bolt/src/test/java/org/neo4j/jdbc/bolt/SampleIT.java

An example of a unit test using that rule class is available at https://github.com/neo4j-contrib/neo4j-jdbc/blob/master/neo4j-jdbc-bolt/src/test/java/org/neo4j/jdbc/bolt/SampleIT.java

这篇关于Neo4j:正确单元测试螺栓驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆