Spring for Cassandra unit

Les Hazlewood edited this page Dec 28, 2015 · 9 revisions

Overview

The Spring TestContext Framework provides annotation-driven unit and integration testing support.

cassandra-unit-spring provides a way to use Cassandra Unit with the Spring TestContext Framework.

Annotations

The magic is in CassandraUnitTestExecutionListener class which implements TestExecutionListener. This listener finds the CassandraUnit annotations:

If you use these annotations by default, you can replace them with @CassandraUnit which is meta-annotated with @EmbeddedCassandra and @CassandraDataSet.

Configuration

In your pom.xml, you have to add the cassandra-unit-spring maven dependency.

  <dependency>
    <groupId>org.cassandraunit</groupId>
    <artifactId>cassandra-unit-spring</artifactId>
    <version>2.2.2.1</version>
  </dependency>

Older versions: 1.2.0.1

Use cases

It's time to use cassandra-unit-spring.

The following snippet uses basic Spring configuration and @CassandraUnit which is the simplest configuration.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners(
  listeners = CassandraUnitTestExecutionListener.class,
  mergeMode = MERGE_WITH_DEFAULTS
)
@CassandraUnit
public class MyCassandraUnitTest {

  @Test
  public void xxx_xxx() {
  }

}

CassandraUnitTestExecutionListener finds the @CassandraUnit annotation and tries to start an Embedded Cassandra with default configuration and load default data set. We explain the default configuration and data set in the next snippet.

This snippet directly uses @CassandraDataSet and @EmbeddedCassandra.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners(
  listeners = CassandraUnitTestExecutionListener.class,
  mergeMode = MERGE_WITH_DEFAULTS
)
@CassandraDataSet
@EmbeddedCassandra
public class MyCassandraUnitTest {

  @Test
  public void xxx_xxx() {
  }

}

CassandraUnitTestExecutionListener finds the @CassandraDataSet and @EmbeddedCassandra annotations and tries to start an Embedded Cassandra with default configuration and load the default data set.

@EmbeddedCassandra default configuration is:

  • configuration: a Cassandra configuration file which is cu-cassandra.yaml by default and provided by cassandra-unit
  • clusterName: name of Cassandra cluster, "Test Cluster" by default
  • host: host of Cassandra cluster; "127.0.0.1" by default
  • port: port of Cassandra cluster; 9142 by default (CQL), use 9171 for Thrift (JSON, YAML or XML)

@CassandraDataSet default configuration is:

  • value: a set of classpath file path; all files must be the same type
  • keyspace: keyspace in which you want to load dataset; by default is "cassandra_unit_keyspace" and only needed CQL case
  • type: type of dataset, cql by default. xml, json, and yaml are also available

The following snippet show you how to customize Cassandra Unit Spring Annotations.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/default-context.xml")
@TestExecutionListeners(
  listeners = CassandraUnitTestExecutionListener.class,
  mergeMode = MERGE_WITH_DEFAULTS
)
@CassandraDataSet(value = "cql/dataset1.cql", keyspace = "mykeyspace")
@EmbeddedCassandra(configuration = "cassandra.yaml", clusterName = "Test Cluster", host = "127.0.0.1", port = 9142)
public class MyCassandraUnitTest {

  @Test
  public void xxx_xxx() {
  }

}

The following snippet shows you how to use annotations with YAML files.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/default-context.xml")
@TestExecutionListeners(
  listeners = CassandraUnitTestExecutionListener.class,
  mergeMode = MERGE_WITH_DEFAULTS
)
@CassandraDataSet(value = { "yaml/dataset1.yaml",  "yaml/dataset2.yaml" }, keyspace = "mykeyspace", type = DataSetFileExtensionEnum.yaml)
@EmbeddedCassandra(configuration = "cassandra.yaml", clusterName = "Test Cluster", host = "127.0.0.1", port = 9142)
public class MyCassandraUnitTest {

  @Test
  public void xxx_xxx() {
  }

}

Have fun with cassandra-unit-spring :)