Databases Reference
In-Depth Information
tx
.
success
();
}
finally
{
tx
.
finish
();
}
return
db
;
}
There are two things of interest in
createDatabase()
. The first is the use of
Imperma
nentGraphDatabase
, which is a lightweight, in-memory version of Neo4j designed
specifically for unit testing. By using
ImpermanentGraphDatabase
, we avoid having to
clear up store files on disk after each test. The class can be found in the Neo4j kernel
test jar, which can be obtained with the following dependency reference:
<dependency>
<groupId>
org.neo4j
</groupId>
<artifactId>
neo4j-kernel
</artifactId>
<version>
${project.version}
</version>
<type>
test-jar
</type>
<scope>
test
</scope>
</dependency>
ImpermanentGraphDatabase
should only be used in unit tests. It is not
a production-ready, in-memory version of Neo4j.
The second thing of interest in
createDatabase()
is the code for adding nodes to a
named index. Cypher doesn't add the newly created nodes to an index, so for now, we
have to do this manually by iterating all the nodes in the sample graph, adding any with
a
name
property to the
users
named index.
Having created a sample graph, we can now write our first test. Here's the test fixture
for testing our social network data model and its queries:
public
class
SocialNetworkTest
{
private
static
GraphDatabaseService
db
;
private
static
SocialNetworkQueries
queries
;
@BeforeClass
public
static
void
init
()
{
db
=
createDatabase
();
queries
=
new
SocialNetworkQueries
(
db
);
}
@AfterClass
public
static
void
shutdown
()