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 이름을 가지고 있다. 


https://github.com/jsevellec/cassandra-unit/blob/master/cassandra-unit-spring/src/main/java/org/cassandraunit/spring/EmbeddedCassandra.java


@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)
@Inherited
@Documented
public @interface EmbeddedCassandra {
  // cassandra configuration file
  String 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 file
  String clusterName() default "Test Cluster";
  String host() default "127.0.0.1";
  // CQL port be default, use 9171 for Thrift
  int port() default 9142;
}


Posted by 김용환 '김용환'