Spring 기반으로 Cassandra를 테스트할 때, @EmbeddedCassandra 를 이용하여 자동으로 Cassandra 서버를 실행시킬 수 있다.
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ CassandraUnitTestExecutionListener.class })
@CassandraDataSet(value = { "simple.cql" })
@EmbeddedCassandra
public class KnightSpringCQLScriptLoadTest {
Session session ;
@Before
public void should_have_started_and_execute_cql_script() throws Exception {
Cluster cluster = Cluster.builder()
.addContactPoints("127.0.0.1")
.withPort(9142)
.build();
session = cluster.connect("cassandra_unit_keyspace");
}
@Test
public void testSelect() {
ResultSet result = session.execute("select * from mytable WHERE id='myKey01'");
assertThat(result.iterator().next().getString("value"), Matchers.is("myValue01"));
}
}
Cassandra의 디폴트 설정은 127.0.0.1:9142 이고 Test Cluster 라는 cluster 이름을 가지고 있다.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)@Inherited@Documentedpublic @interface EmbeddedCassandra {// cassandra configuration fileString configuration() default EmbeddedCassandraServerHelper.DEFAULT_CASSANDRA_YML_FILE;// the following settings is needed to load dataset, you must use the same value that can be found in configuration fileString clusterName() default "Test Cluster";String host() default "127.0.0.1";// CQL port be default, use 9171 for Thriftint port() default 9142;}