Over a million developers have joined DZone.

NoSQLUnit 0.3.2 Released

DZone 's Guide to

NoSQLUnit 0.3.2 Released

· Java Zone ·
Free Resource
NoSQLUnit is a JUnit extension to make writing unit and integration tests of systems that use NoSQL backend easier. Visit official page for more information.

In 0.3.2 release, one new NoSQL system is supported and is Neo4j.

Neo4j is a high-performance, NoSQL graph database with all the features of a mature and robust database.

As all databases supported by NoSQLUnit, two set of rules are provided to write Neo4j tests:

First set of JUnit Rules are those responsible of managing database lifecycle; basically starting and stopping Neo4j.

  • Embedded: com.lordofthejars.nosqlunit.neo4j.EmbeddedNeo4j
  • Managed Wrapping: com.lordofthejars.nosqlunit.neo4j.ManagedWrappingNeoServer
  • Managed: com.lordofthejars.nosqlunit.neo4j.ManagedNeoServer

Depending on kind of test you are implementing (unit test, integration test, deployment test, ...) you will require an embedded approach, managed approach or   remote approach. Note that there is no implementation of a Neo4j in-memory database at this time, but embedded strategy for unit tests will be the better one. As Neo4j developers will know, you can start remote Neo4j server by calling start/stop scripts ( Managed way) or by using an embedded database wrapper by a server ( Managed Wrapping way), both of them are supported by NoSQLUnit.

Second set of rules are those responsible of maintaining database into known state;
  • NoSQLUnit Management: com.lordofthejars.nosqlunit.neo4j.Neo4jRule
And finally default dataset file format in Neo4j is GraphML. GraphML is a comprehensive and easy-to-use file format for graphs.
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
    <key id="attr1" for="edge" attr.name="attr1" attr.type="float"/>
    <key id="attr2" for="node" attr.name="attr2" attr.type="string"/>
    <graph id="G" edgedefault="directed">
        <node id="1">
            <data key="attr2">value1</data>
        <node id="2">
            <data key="attr2">value2</data>
        <edge id="7" source="1" target="2" label="label1">
            <data key="attr1">float</data>

We will use the example of finding Neo's friends as an example of how to write unit tests for systems that uses Neo4j databases as backend.

First of all dataset used to maintain Neo4j into known state
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
    <key id="name" for="node" attr.name="name" attr.type="string"/>
    <key id="age" for="edge" attr.name="age" attr.type="int"/>
    <graph id="G" edgedefault="directed">
        <node id="1">
            <data key="name">Thomas Anderson</data>
        <node id="2">
            <data key="name">Trinity</data>
        <node id="3">
            <data key="name">Morpheus</data>
        <node id="4">
            <data key="name">Agent Smith</data>
        <node id="5">
            <data key="name">The Architect</data>
        <edge id="1" source="0" target="1" label="NEO_NODE">
        <edge id="2" source="1" target="2" label="KNOWS">
            <data key="age">3</data>
        <edge id="3" source="1" target="3" label="KNOWS">
            <data key="age">5</data>
        <edge id="4" source="2" target="3" label="KNOWS">
            <data key="age">18</data>
        <edge id="5" source="3" target="4" label="KNOWS">
            <data key="age">20</data>
        <edge id="6" source="4" target="5" label="CODED_BY">
            <data key="age">20</data>

and finally the test case:
public class WhenNeoFriendsAreRequired {

public static EmbeddedNeo4j embeddedNeo4j = newEmbeddedNeo4jRule().build();

public Neo4jRule neo4jRule = new Neo4jRule(newEmbeddedNeoServerConfiguration().build(), this);

private GraphDatabaseService graphDatabaseService;

@UsingDataSet(locations="matrix.xml", loadStrategy=LoadStrategyEnum.CLEAN_INSERT)
public void all_direct_and_inderectly_friends_should_be_counted() {
MatrixManager matrixManager = new MatrixManager(graphDatabaseService);
int countNeoFriends = matrixManager.countNeoFriends();
assertThat(countNeoFriends, is(3));


Next release will support Cassandra. Stay in touch with the project and of course I am opened to any ideas that you think that could make NoSQLUnit better.

We Keep Learning,

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}