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



我有两个不同的测试类,一个测试我编写的模块,另一个测试我开发的用户定义函数。这两个测试以不同的方式实例化 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数据库?

看看 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 和类与要注册的过程/函数一起传递,这应该为您的测试类设置所有内容。

最新更新