DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > NoSQLUnit 0.3.2 Released

NoSQLUnit 0.3.2 Released

Alex Soto user avatar by
Alex Soto
·
Aug. 08, 12 · Java Zone · Interview
Like (0)
Save
Tweet
4.11K Views

Join the DZone community and get the full member experience.

Join For Free
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>
        <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.
unit test Database Neo4j

Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is High Cardinality?
  • Kubernetes Service Types Explained In-Detail
  • Are All Kubernetes Ingresses the Same?
  • Purpose-Driven Microservice Design

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo