mercredi 10 août 2011

CassandraUnit : a Java test framework to develop with Cassandra



Hi everyone,

Let me present you CassandraUnit!

Why?

I work for few months on a new Java application with a Cassandra database backend.

As I'm A TDD Lover, I'd like to developp in a pure TDD Style combined with continuous integration system like Jenkins.

The difficulty when using a database backend is how to write isolated unit Test. One of solutions is to :
  • first : embed an instance of your database in your unit test.
  • next : load data into your database to setup your unit test.
  • finally : execute your test with your data.


In a Relational database world, there is some tools to help you like DBUnit. As Cassandra is a relatively young product (but so beautiful :-)), I didn't find some tools to help me doing what I want.

CassandraUnit is born.

What does it do?

CassandraUnit, for now, does :
  • embed and start a Cassandra instance in your code if you want.
  • load data from a dataset (xml).
A very little example?


  • create an xml dataset like that "simpleDataSet.xml" :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<keyspace xmlns="http://xml.dataset.cassandraunit.org">
    <name>beautifulKeyspaceName</name>
    <columnFamilies>
        <columnFamily>
            <name>beautifulColumnFamilyName</name>
            <row>
                <key>key10</key>
                <column>
                    <name>name11</name>
                    <value>value11</value>
                </column>
                <column>
                    <name>name12</name>
                    <value>value12</value>
                </column>
            </row>
            <row>
                <key>key20</key>
                <column>
                    <name>name21</name>
                    <value>value21</value>
                </column>
            </row>
        </columnFamily>
    </columnFamilies>
</keyspace>

  • create a JUnit test case witch extends AbstractCassandraUnit4TestCase :
package org.cassandraunit;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.cassandraunit.dataset.IDataSet;
import org.cassandraunit.dataset.xml.ClassPathXmlDataSet;
import org.junit.Test;

public class AutomaticallyStartAndLoadSimpleDataSetTest extends AbstractCassandraUnit4TestCase {

    @Override
    public IDataSet getDataSet() {
        return new ClassPathXmlDataSet("simpleDataSet.xml");
    }

    @Test
    public void shouldHaveLoadASimpleDataSet() throws Exception {
        assertThat(getKeyspace(), notNullValue());
        assertThat(getKeyspace().getKeyspaceName(), is("beautifulKeyspaceName"));
    }

}

And now?

It was just a very little example. CassandraUnit allows to make more complex dataSet (Super column, type definition, ...).
I've just published it on github. It is published under LGPL v3.0 license.
The first release will be available on maven central repository in the coming hours.
As it's the first release, It probably still missing a lot of functionnality but it was time for me to published it.

You want to go further?

and it's normal :-)

Here you can find more informations :

You can follow it on twitter @cassandraunit

I hope it will help you as it helps me every day on my project.

Aucun commentaire: