将调试器附加到neo4j过程jar

时间:2020-07-17 11:10:44

标签: java unit-testing testing neo4j cypher

我正在开发a neo4j procedure in java。我可以使用下面的自定义数据对其进行测试。

@Test
public void commonTargetTest2() {
    // This is in a try-block, to make sure we close the driver after the test
    try (Driver driver = GraphDatabase.driver(embeddedDatabaseServer.boltURI(), driverConfig);
            Session session = driver.session()) {
        // And given I have a node in the database
        session.run(
                "CREATE (n1:Person {name:'n1'}) CREATE (n2:Person {name:'n2'}) CREATE (n3:Person {name:'n3'}) CREATE (n4:Person {name:'n4'}) CREATE (n5:Person {name:'n5'})"
                        + "CREATE (n6:Person {name:'n6'}) CREATE (n7:Person {name:'n7'}) CREATE (n8:Person {name:'n8'}) CREATE (n9:Person {name:'n9'}) CREATE (n10:Person {name:'n10'})"
                        + "CREATE (n11:Person {name:'n11'}) CREATE (n12:Person {name:'n12'}) CREATE (n13:Person {name:'n13'})"
                        + "CREATE (n14:Person {name:'n14'}) CREATE "
                        + "(n1)-[:KNOWS]->(n6),(n2)-[:KNOWS]->(n7),(n3)-[:KNOWS]->(n8),(n4)-[:KNOWS]->(n9),(n5)-[:KNOWS]->(n10),"
                        + "(n7)-[:KNOWS]->(n11),(n8)-[:KNOWS]->(n12),(n9)-[:KNOWS]->(n13),"
                        + "(n11)-[:KNOWS]->(n14),(n12)-[:KNOWS]->(n14),(n13)-[:KNOWS]->(n14);");

        // name of the procedure I defined is "p1", below I'm calling it in cypher
        StatementResult result = session
                .run("CALL p1([1,3], [], 3, 0) YIELD nodes, edges return nodes, edges");

        InternalNode n = (InternalNode) result.single().get("nodes").asList().get(0);
        assertThat(n.id()).isEqualTo(13);
    }
}

这很好用,但是数据是使用CREATE语句新生成的,并且非常小。我想用现有的neo4j数据库服务器测试我的过程。这样我就可以查看使用真实/大数据的过程的性能/结果。

我也可以使用以下代码来实现。我可以连接到正在运行的neo4j数据库。

@Test
public void commonTargetTestOnImdb() {
    // This is in a try-block, to make sure we close the driver after the test
    try (Driver drv = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "123"));
            Session session = drv.session()) {

        // find 1 common downstream of 3 nodes
        StatementResult result = session.run(
                "CALL commonStream([1047255, 1049683, 1043696], [], 3, 2) YIELD nodes, edges return nodes, edges");

        InternalNode n = (InternalNode) result.single().get("nodes").asList().get(0);
        assertThat(n.id()).isEqualTo(5);
    }

}

现在,我的问题是,如果我连接到现有数据库,则无法调试过程的代码。我打包了一个JAR文件,并将其放在neo4j数据库的plugin文件夹中,以便neo4j可以调用我的过程。我想我应该调试JAR文件。我正在使用vscode和Java扩展来调试和运行测试。如何使用vscode调试JAR文件?

1 个答案:

答案 0 :(得分:0)

出于记录,我找到了一种调试neo4j存储过程的方法。我正在使用Java8。我使用了IntelliJ想法。我将配置dbms.jvm.additional=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005添加到 neo4j.conf 文件

在IntelliJ IDEA中,我添加了用于远程调试的新配置。 Remote debugging configuration for IntelliJ IDEA

请注意,Java 9+中的语法是不同的。最后,使用address=*:5005来指定端口参数。这里有关于它的文章。 https://stackoverflow.com/a/62754503/3209523