Issue
In my Spring Boot 1.5.1 application I'm going to write unit/integration tests for Cassandra related logic.
I have added folowing Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
The default Spring Boot Cassandra configuration is going to connect with a real Cassandra DB server.
Is there any options in Spring/Spring Boot in order to configure my tests to use embedded Cassandra server ? If so, could you please show the required configuration.
Solution
We use on the project Cassandra + Spring Boot. Here are the steps which worked for us:
a) Configure you test like this
import org.cassandraunit.spring.CassandraDataSet;
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener;
import org.cassandraunit.spring.CassandraUnitTestExecutionListener;
import org.cassandraunit.spring.EmbeddedCassandra;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
@TestExecutionListeners(listeners = {
CassandraUnitDependencyInjectionTestExecutionListener.class,
CassandraUnitTestExecutionListener.class,
ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class
})
@EmbeddedCassandra(timeout = 60000)
@CassandraDataSet(value = {"bootstrap_test.cql"}, keyspace = "local_test")
public abstract class BaseTest {
b) in your src/test/resources/application.properties, add this (please note, embedded cassandra starts on port 9142, but not on default 9042)
spring.data.cassandra.port=9142
spring.data.cassandra.keyspace-name=local_test
c) Create empty file bootstrap_test.cql in src/test/resources
d) Add to your pom.xml
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit-spring</artifactId>
<version>${cassandra-unit.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit-spring</artifactId>
<version>${cassandra-unit.version}</version>
</dependency>
This should be enough to run your tests with Embedded Cassandra. Hope it helps.
Answered By - walv
Answer Checked By - Senaida (JavaFixing Volunteer)