Database Reference
In-Depth Information
} catch (Exception e) {
System.err.println("Cannot cleanup Test
Database....check Logs for Stack Traces");
throw e;
}
}
}
In the preceding code, we have defined two methods
createTestDatabase
and
cleanupTestDatabase
, where the former creates a test database and the
latter shuts down the database. These methods are automatically invoked by the
JUnit framework and the annotations
@Before
and
@After
help JUnit frame-
work to identify these setup and cleanup methods. Also, these methods are in-
voked before and after execution of each JUnit test case.
Neo4j provides a test database factory, which creates a test database with minim-
um configuration and helps developers to focus on writing unit test cases, rather
than setting up the database. Here is that piece of code, which creates a test data-
base
new TestGraphDatabaseFact-
ory().newImpermanentDatabase();
4. Next, open your console and browse your project root, that is,
MyNeo4jSamples
and execute
$M2_HOME/bin/mvn test
for executing
your test cases.
5. Your build should fail, because there are no unit tests to be executed. So now, let's
add a unit test case by the name of
nodeCreationWithLabel
in
Neo4jTest.java
and add the following code:
@org.junit.Test
public void nodeCreationWithLabel() {
//Open a Transaction
try(Transaction transaction=graphDb.beginTx()){
String testLabel="TestNode";
//Create a Node with Label in the neo4j Database
graphDb.createNode(DynamicLabel.label(testLabel));
//Execute Cypher query and retrieve all Nodes
from Neo4j Database
ExecutionEngine engine = new
ExecutionEngine(graphDb, StringLogger.SYSTEM);
String query = "MATCH (n) return n";