How to use it in your code

Raphaël Brugier edited this page Nov 18, 2015 · 9 revisions

There is different possibilities to use CassandraUnit. There is two main features :

  • The first is to start an embedded Cassandra Server in your JVM.
  • The second is to load Data into a Cassandra Server (embedded or not).

You can use both features separately or together.

This section explains how to use API. The dataSet creation is explained in this section : How to create a DataSet?

1. Native Approach

One simple way is to use native Api :

Manage an embedded Cassandra server

  • To start an embedded Cassandra Server

You just have to :

EmbeddedCassandraServerHelper.startEmbeddedCassandra();

If this method has been already called, nothing will happen, Cassandra still be started. By default, the embedded Cassandra server starts on 127.0.0.1, ports 9171 (Thrift) and 9142 (Native).

If you want to start Cassandra server on a random available port, you can use a provided alternative cassandra yaml configuration file:

EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE);

It's also possible to start the embedded Cassandra server with your own cassandra yaml configuration file (to set another port for example), you can use give your cassandra file. This file has to be in your classpath (since the 1.0.3.1) :

EmbeddedCassandraServerHelper.startEmbeddedCassandra(String yourCassandraYamlFile);

When you start an embedded cassandra server, it create a new empty instance.

  • Clean an embedded Cassandra Server :

To clean an embedded Cassandra Server which is already started you just have to (cleaning consist in dropping all existing keyspace except "system") :

EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
  • Stop an embedded Cassandra Server : (since the 1.0.3.1)

You just have to (since the 1.0.3.1) :

EmbeddedCassandraServerHelper.stopEmbeddedCassandra();

use cleanEmbeddedCassandra is faster than stopping and starting a new instance but both possibilites works.

Load data from a data set

To load data into a started Cassandra server (embedded or not) :

DataLoader dataLoader = new DataLoader("TestCluster", "localhost:9171");
dataLoader.load(new ClassPathXmlDataSet("simpleDataSet.xml"));

2. Using AbstractCassandraUnit4TestCase

CassandraUnit provides an Abstract JUnit Test Case to extend to simplify the use of Cassandra for extends test classes.

You have to :

  • create a test class AbstractCassandraUnit4TestCase
  • implement method getDataSet which has to return an instance of a DataSet.

In your test case, by inheritance, you hava access to getKeyspace(). This method gives you a Hector Keyspace on your Cassandra server.

You can see an example here :

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 AutomaticallyStartAndLoadExtendedDataSetTest extends AbstractCassandraUnit4TestCase {

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

    @Test
    public void shouldHaveLoadAnExtendDataSet() throws Exception {
        //here, a Cassandra server is started and your data from extendedDataSet.xml has been loaded
        // you can query or do what you want
        assertThat(getKeyspace(), notNullValue());
        assertThat(getKeyspace().getKeyspaceName(), is("otherKeyspaceName"));
    }

}

3. Using Junit4 @Rule

It's possible to use CassandraUnit without enforcing the use of AbstractCassandraUnit4TestCase by using @Rule You have to declare a CassandraUnit attribute in your test with the @Rule annotation

You can see an example here :

package org.cassandraunit;

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

import org.cassandraunit.CassandraUnit;
import org.cassandraunit.dataset.xml.ClassPathXmlDataSet;
import org.junit.Test;
import org.junit.Rule;

public class AutomaticallyStartAndLoadExtendedDataSetTest {
    @Rule
    public CassandraUnit cassandraUnit = new CassandraUnit(new ClassPathXmlDataSet("extendedDataSet.xml"));

    @Test
    public void shouldHaveLoadAnExtendDataSet() throws Exception {
        //here, a Cassandra server is started and your data from extendedDataSet.xml has been loaded
        // you can query or do what you want
        assertThat(cassandraUnit.keyspace, notNullValue());
        assertThat(cassandraUnit.keyspace.getKeyspaceName(), is("otherKeyspaceName"));
    }

}