May 28, 2015

Cassandra Unit Testing

Cassandra Unit is a library which can integrate with your Java application and help dynamically spin up an embedded Cassandra instances when needed. Not only can the Cassandra instance be started and stopped programatically, but you can also bootstrap the cluster by providing DML and DDL CQL(cassandra query language) commands in a file to prepare the instance for any sort of unit testing.   What purpose does this library serve? 1. Biggest advantage is in Unit and Integration testing. Developers or CI/CD tools no longer need to depend on a centralized database. 2. Developers can run all kinds of test scenarios on their code and test you their DML/DDL queries before executing them on main Cassandra Cluster. Which means faster development, reduced latency and dependency. 3. Encourages test driven development of database interacting functionalities.

Steps for Spring JUnit :

Step #1
Include the following in the pom.xml of the Maven Java Project.
	
<dependency>
   <groupId>org.cassandraunit</groupId>
   <artifactId>cassandra-unit-spring</artifactId>
   <version>2.1.3.1</version>
   <scope>test</scope>
</dependency>


<dependency>
     <groupId>org.cassandraunit</groupId>
     <artifactId>cassandra-unit</artifactId>
     <version>2.0.2.1</version>
</dependency>
	
Step #2
Annotate your Junit class with the following
	
	
@SpringApplicationConfiguration(classes = CassandraConfiguration.class)

@TestExecutionListeners({ CassandraUnitDependencyInjectionTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class })

@CassandraDataSet(value = { "create-table.cql" }, keyspace = "your-cassandra-keyspace")

@EmbeddedCassandra

@Configuration

@RunWith(SpringJUnit4ClassRunner.class)

	
Step #3
Place your DDL/DML CQL queries in a file named 'create-table.cql' under /resource folder of your TEST and the run the unit test.

Additional Information

Jmeter plugin for Cassandra developed by Netflix can be used to create load test scripts for Cassandra. This plugin for JMeter allow one to define and execute CQL commands on a Cassandra cluster directly from JMeter.