在实例化嵌入式数据库的同一单元测试中加载UDF和自定义模块

时间:2017-04-06 16:17:18

标签: java junit neo4j

我有两个不同的测试类,一个测试我编写的模块,另一个测试我开发的用户定义的函数。这两个测试以不同的方式实例化Neo4j用于测试目的。模块测试是这样的:

class ModuleTest
{
    GraphDatabaseService database;

    @Before
    public void setUp()
    {
        String confFile = this.getClass().getClassLoader().getResource("neo4j-module.conf").getPath();
        database = new TestGraphDatabaseFactory()
                .newImpermanentDatabaseBuilder()
                .loadPropertiesFromFile(confFile)
                .newGraphDatabase();
    }
}

虽然UDF测试类以这种方式实例化其嵌入式数据库:

public class UdfTest
{
    @Rule
    public Neo4jRule neo4j = new Neo4jRule()
        .withFunction(Udf.class);

    @Test
    public void someTest() throws Throwable
    {
        try (Driver driver = GraphDatabase.driver(neo4j.boltURI() , Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig())) {
            Session session = driver.session();
            //...
        }
    }
}

这里的问题是,在第一种形式中,UDF没有注册,第二种形式是模块。我的问题是;如何为我的模块和UDF加载的测试启动嵌入式Neo4j数据库?

1 个答案:

答案 0 :(得分:1)

了解APOC程序如何在其测试类中加载过程和函数。他们在setUp()中调用实用程序方法:

public static void registerProcedure(GraphDatabaseService db, Class<?>...procedures) throws KernelException {
    Procedures proceduresService = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Procedures.class);
    for (Class<?> procedure : procedures) {
        proceduresService.registerProcedure(procedure);
        proceduresService.registerFunction(procedure);
    }
}

只需传递GraphDatabaseService和带有要注册的过程/函数的类,这应该为您的测试类设置一切。