CassandraUnit是一个Cassandra数据操作的单元测试框架,其功能与关系型数据库测试框架DBunit类似,只是其测试对象是Cassandra,下面是一个最简单使用例子:
创建一个用于存储数据的xml文件,命名为 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>
然后再创建一个单元测试类,继承自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")); } }