版本 2.2.3 的 Neo4j 非托管扩展单元测试

Neo4j Unmanaged Extension unit testing for version 2.2.3

我喜欢为我为小型 Neo4j 项目编写的非托管扩展创建单元测试。

GraphDatabaseService db = new TestGraphDatabaseFactory()
                .newImpermanentDatabaseBuilder()
                .setConfig(GraphDatabaseSettings.pagecache_memory, "512M")
                .setConfig( GraphDatabaseSettings.string_block_size, "60" )
                .setConfig( GraphDatabaseSettings.array_block_size, "300" )
                .newGraphDatabase();

我想在我的@Before 测试中使用类似于上面代码的方法 class - 我理解这是编写单元测试的新方法。

我想问:

  1. 如何使用配置设置关闭自动身份验证
  2. 如何注册我们的分机

我设法用下面的代码实现了我的目标,但我收到了一堆已弃用的警告。

ImpermanentGraphDatabase db = new ImpermanentGraphDatabase();
ServerConfigurator config = new ServerConfigurator(db);
        config.configuration().setProperty("dbms.security.auth_enabled", false);
        config.getThirdpartyJaxRsPackages().add(new ThirdPartyJaxRsPackage("com.mine.graph.neo4j", "/extensions/mine"));
testBootstrapper = new WrappingNeoServerBootstrapper(db, config);
testBootstrapper.start();

我的解决方案是基于 Neo4j 测试 classes 创建您自己的 TestServer,以便您可以设置属性并加载 UMX

public class Neo4jTestServer  {
private AbstractNeoServer server;
private GraphDatabaseService database;

public Neo4jTestServer() {

    try {
        ServerControls controls = TestServerBuilders.newInProcessBuilder()
                .withExtension("/fd", "org.flockdata.neo4j")
                .withConfig("dbms.security.auth_enabled", "false")
                .newServer();

        initialise(controls);

    } catch (Exception e) {
        throw new RuntimeException("Error starting in-process server",e);
    }

}

private void initialise(ServerControls controls) throws Exception {

    Field field = InProcessServerControls.class.getDeclaredField("server");
    field.setAccessible(true);
    server = (AbstractNeoServer) field.get(controls);
    database = server.getDatabase().getGraph();
}

/**
 * Retrieves the base URL of the Neo4j database server used in the test.
 *
 * @return The URL of the Neo4j test server
 */
public String url() {
    return server.baseUri().toString();
}}

您还需要一个 InProcessServer 来 return 您的带有 UMX

的 TestServer
public class Neo4jInProcessServer implements Neo4jServer{
private Neo4jTestServer testServer;

public Neo4jInProcessServer()  {
    this.testServer = new Neo4jTestServer();
}

public String url() {
    return testServer.url();
}}

最后,您需要 return 配置中的 InProcessServer class

    @Override
public Neo4jServer neo4jServer() {

    return new Neo4jInProcessServer();
}