Reputation: 25802
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.
Upvotes: 9
Views: 15917
Reputation: 24558
The solution by OP is no longer applicable to latest versions of Spring Boot, since CassandraUnitDependencyInjectionTestExecutionListener
comes from cassandra-unit-spring that only works with JUnit 4, and the world has moved onto JUnit 5.
I've created a cassandra-unit-spring library that starts the Cassandra server and makes the ports available as Spring Boot environment properties.
Upvotes: 1
Reputation: 2938
I know it is an old topic BUT there are two approaches to use embedded Cassandra with Spring.
I have created a sample application and you can check out the following Github repository.
Note: the Embedded server start on the 9142 port NOT on the 9042 port !!!
Step 1:
Add the following maven plugins to pom.xml
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cassandra-maven-plugin</artifactId>
<version>3.6</version>
<executions>
<execution>
<goals>
<goal>start</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<startNativeTransport>true</startNativeTransport>
</configuration>
</plugin>
Step 2:
Create cassandra/cql directory at the root level of your project. From this directory the Embedded Cassandra server will initialize your database.
Important: All files from this directory are .cql files.
Example: cassandra/cql/load.cql
Step 3:
Create integration tests.
Important: Only the test classes with IT suffix are interpreted by surfire plugin as integration tests.
Step 4:
mvn verify to run the integration tests
Upvotes: 1
Reputation: 2868
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.
Upvotes: 12
Reputation: 18119
There is no embedded Apache Cassandra support in Spring Boot available and it's not planned. There's little demand for embedded Apache Cassandra on the one hand, on the other hand, Apache Cassandra comes with a lot of dependencies that conflict with Boot's other dependencies.
Take a look at Cassandra Unit.
You can also build an own test rule when using JUnit tests that gives you full control over the Apache Cassandra version, runtime behavior and so on. Take a look at a possible implementation: CassandraRule.java.
Upvotes: 3