neo4jgraphaware

Incapable of running unit tests because of CREATE INDEX and CREATE CONSTRAINT


I've developed a module for Neo4j using Graphaware package. As a part of my module, I want to make sure that some indices and/or constraints are present in the database. To this end, I use BaseTxDrivenModule.initialize method to run a couple of Cypher statements:

@Override
public void initialize(GraphDatabaseService database)
{
    database.execute("CREATE CONSTRAINT ON (n:`Label`) ASSERT n.`id` IS UNIQUE;");
    database.execute("CREATE INDEX ON n:`Label2`(`id`) IS UNIQUE;");
}

These statements run successfully in production when I deploy the module in a server instance of Neo4j. But when I want to run the unit tests, as a part of build process, the execution hangs and never finishes. And when I omit the initialize method, it goes on without any error.

The worst part is that I have to build the package like: mvn package -Dmaven.test.skip=true or it won't build anything.

So my question is, why? And how can I fix this problem?

Here's a sample project demonstrating the issue:

https://github.com/ziadloo/test_neo4j_module

Just clone it and run mvn package and you'll see that the tests never finish.


Solution

  • There is no guarantee that the Runtime is started during your test, you'll have to assert it by invoking the waitUntilStarted method.

    @Before
        public void setUp() {
            database = new TestGraphDatabaseFactory()
                .newImpermanentDatabaseBuilder()
                .loadPropertiesFromFile(this.getClass().getClassLoader().getResource("neo4j-module.conf").getPath())
                .newGraphDatabase();
    
            getRuntime(database).waitUntilStarted();
    
            registerShutdownHook(database);
        }
    

    I would suggest you take a look at some test cases in the neo4j-uuid module for eg.