Testing Neo4j with Bolt

For a project I’m working on with my colleague Joost den Boer we are investigating the use of graph databases in the form of Neo4j. The current version of Neo4j is 2.3, however last christmas 3.0.0.M02 was released with one of the key features the introduction of the Bolt driver.

Neo4j evolved from being available only as an embedded process in a JVM-based application to being available to a server with a series of RESTful APIs to talk to it. This is all good and well but anyone can tell you that Neo4j’s query performance over its REST services was not as stellar as you’d hope.

Bolt as a technology is exciting because it is designed as a high-performance access through a binary protocol to your graph database. The end goal of Bolt is that it will consolidate and refine all the ways of doing work with Neo4j, providing more consistent access with a pleasant separation of responsibility.

As performance is important to us and Bolt will provide a way of communicating with both an embedded Neo4j instance and a server we decided to give it a go. One of the main questions was however, how can you write integration tests with the application starting it’s own Neo4j instance embedded and connecting to it with Bolt?

Turned out this was quite easy. We are making a Grails app, so this is Groovy.

String myBoltAddress = "localhost:7888" // Any port you'd like
ServerControls neoServer = TestServerBuilders.newInProcessBuilder()
        .withConfig( connector(0, BoltKernelExtension.Settings.enabled), "true")
        .withConfig( connector(0, BoltKernelExtension.Settings.socket_address), myBoltAddress)
        .newServer()

Driver boltDriver = GraphDatabase.driver("bolt://" + myBoltAddress)

With the Bolt driver instance you can then run any statement you’d like in Cypher as such:

void "get some data"() {
    when:
    def result = boltDriver.session().run("MATCH (n) RETURN n LIMIT 10")

    then:
    result.list().size() == 10 // Obviously you need to have at least ten nodes.
}

Leave a Reply