我喜欢为我为一个小型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测试类中使用类似于上面代码的方法 - 我理解这是编写单元测试的新方法。
我想问:
我设法通过下面的代码实现我的目标,但是我收到了大量已弃用的警告。
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();
答案 0 :(得分:3)
我的解决方案是根据Neo4j测试类创建自己的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来返回带有UMX的TestServer
public class Neo4jInProcessServer implements Neo4jServer{
private Neo4jTestServer testServer;
public Neo4jInProcessServer() {
this.testServer = new Neo4jTestServer();
}
public String url() {
return testServer.url();
}}
最后,您需要从配置类
返回InProcessServer @Override
public Neo4jServer neo4jServer() {
return new Neo4jInProcessServer();
}