-
Notifications
You must be signed in to change notification settings - Fork 2
How to use it in your code
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?
One simple way is to use native Api :
- 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 :
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();
At the moment there's no way to stop Cassandra. Cassandra is started in the same JVM as your tests. It will be shutdown when the JVM used by your tests is shutdown
To load data into a started Cassandra server (embedded or not) :
DataLoader dataLoader = new DataLoader("TestCluster", "localhost:9171");
dataLoader.load(new ClassPathCQLDataSet("simpleDataSet.cql"));
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 have access to getSession()
. This method gives you a Datastax Cassandra java driver CqlSession
You can see an example here :
package org.cassandraunit.test.cql;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import org.cassandraunit.AbstractCassandraUnit4CQLTestCase;
import org.cassandraunit.dataset.CQLDataSet;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class CQLScriptLoadWithAbstractTestCaseTest extends AbstractCassandraUnit4CQLTestCase {
@Override
public CQLDataSet getDataSet() {
return new ClassPathCQLDataSet("simple.cql","keyspaceNameToCreate");
}
@Test
public void should_have_started_and_execute_cql_script() throws Exception {
ResultSet result = getSession().execute("select * from mytable WHERE id='myKey01'");
assertThat(result.iterator().next().getString("value"), is("myValue01"));
}
}
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.test.cql;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import org.cassandraunit.CassandraCQLUnit;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class CQLScriptLoadWithJunitRuleTest {
@Rule
public CassandraCQLUnit cassandraCQLUnit =
new CassandraCQLUnit(new ClassPathCQLDataSet("simple.cql","keyspaceNameToCreate"));
@Test
public void should_have_started_and_execute_cql_script() throws Exception {
ResultSet result = cassandraCQLUnit.session.execute("select * from mytable WHERE id='myKey01'");
assertThat(result.iterator().next().getString("value"), is("myValue01"));
}
}