NoSQLUnit 0.3.2 Released
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
Second set of rules are those responsible of maintaining database into known state;
- NoSQLUnit Management: com.lordofthejars.nosqlunit.neo4j.Neo4jRule
<?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>
<node id="2">
<data key="attr2">value2</data>
</node>
<edge id="7" source="1" target="2" label="label1">
<data key="attr1">float</data>
</edge>
</graph>
</graphml>
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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<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>
<node id="2">
<data key="name">Trinity</data>
</node>
<node id="3">
<data key="name">Morpheus</data>
</node>
<node id="4">
<data key="name">Agent Smith</data>
</node>
<node id="5">
<data key="name">The Architect</data>
</node>
<edge id="1" source="0" target="1" label="NEO_NODE">
</edge>
<edge id="2" source="1" target="2" label="KNOWS">
<data key="age">3</data>
</edge>
<edge id="3" source="1" target="3" label="KNOWS">
<data key="age">5</data>
</edge>
<edge id="4" source="2" target="3" label="KNOWS">
<data key="age">18</data>
</edge>
<edge id="5" source="3" target="4" label="KNOWS">
<data key="age">20</data>
</edge>
<edge id="6" source="4" target="5" label="CODED_BY">
<data key="age">20</data>
</edge>
</graph>
</graphml>
and finally the test case: public class WhenNeoFriendsAreRequired {
@ClassRule
public static EmbeddedNeo4j embeddedNeo4j = newEmbeddedNeo4jRule().build();
@Rule
public Neo4jRule neo4jRule = new Neo4jRule(newEmbeddedNeoServerConfiguration().build(), this);
@Inject
private GraphDatabaseService graphDatabaseService;
@Test
@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,
Alex.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:





