unit-testingneo4jneo4j-shell

Connect to embedded Neo4j during test debugging


I'm using embedded Neo4j for JUnit tests with the following rule configuration:

@Rule
public Neo4jRule neo4jRule = new Neo4jRule()
        .withConfig("dbms.connector.1.enabled", "true")
        .withConfig("dbms.connector.1.listen_address", "localhost:4710")
        .withConfig("dbms.connector.1.type", "HTTP")
        .withConfig("dbms.connector.bolt.enabled", "true")
        .withConfig("dbms.connector.bolt.listen_address", ":4711")
        .withConfig("apoc.autoIndex.enabled", "true")
        .withConfig("ShellSettings.remote_shell_enabled", "true")
        .withConfig("ShellSettings.remote_shell_port", "5555")
        .withProcedure(apoc.index.FulltextIndex.class)
        .withProcedure(apoc.index.FreeTextSearch.class)
        .withProcedure(apoc.cypher.Cypher.class);

Now I would like to connect via cypher-shell during debugging (breakpoint set) in order to take a look what is actually in the test database at some point. Unfortunately, neither cypher-shell -a localhost:4711 nor neo4j-shell -port 5555 get the connection. The first one doesn't return (remains pending), the second one returns Connection refused. What am I missing? Any idea on how to get a connection?


Solution

  • When a breakpoint hit during debugging, the embedded server also stops responding.

    If you want to look at the state of your DB during unit tests, you need to pause the execution without the breakpoint. My solution is not exactly what you want but you can try this

    @Test
    public void yourTest() throws IOException {
        try {
           System.out.pritnln(neo4j.httpURI());
           Thread.sleep(100000L);
        } catch (Exception e) {
           e.printStackTrace();
        }
    }
    

    The above code will pause for 100s, you can click the link in console to open Neo4j browser and query the DB.