1

I need to start a Embedded cassandra instance to perform some operations on a Cassandra keyspace via Unit tests. Programming Language is Java. What are the options to start an embedded cassandra?

I used the mojo maven plugin, however after starting the instance using following command, I do not see the cassandra instance started at default port 9042 on localhost. Plugin:http://www.mojohaus.org/cassandra-maven-plugin/usage.html Command to start: mvn cassandra:run -Dcassandra.nativeTransportPort=9042

Is there something missing in terms of usage or do I need to use something different?

Thanks Jyothi

Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
Jyothi
  • 71
  • 1
  • 3

4 Answers4

5

It depends on what you are trying to test. Cassandra Maven Plugin (of which I was one of the original authors) and Cassandra Unit both do something very similar in that they start an in-JVM instance of Cassandra.

This works great if you just want to do some CRUD stuff in you integration tests, but really does not help if you want to test scenarios like consistency level failures and retries in the face of various failure cases, particularly for multiple datacenters. And Cassandra is just a beast at this point so it means you need a lot of memory for you tests to run.

To really verify things like the above, I recommend using Simulacron: https://github.com/datastax/simulacron/blob/master/doc/java_api/README.md

And an example integration: https://github.com/datastax/java-driver/blob/9f0d89799a8a1e4cd1022dd7c43333924c36a648/integration-tests/src/test/java/com/datastax/oss/driver/api/core/ProtocolVersionMixedClusterIT.java

This is what the driver teams use to test behavior scenarios, though they still rely on a CCM test bridge (also an option) for a lot of block and tackle stuff. For both, the way it's plumbed into maven in that project should be used as an example of best practices:

https://github.com/datastax/java-driver/blob/3.x/pom.xml#L749-L816

And profile switching for such: https://github.com/datastax/java-driver/blob/3.x/pom.xml#L925-L947

And using the profiles: https://github.com/datastax/java-driver/blob/3.x/driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java#L146

To really get your head around it, I recommend pulling down the driver project and picking it apart to see how this is all put together. Probably the biggest win is that this whole project does not have any dependency on Cassandra code.

zznate
  • 1,898
  • 13
  • 12
  • I said "in-JVM" but pretty sure both can/do fork a JVM as well (it's been a long time since I looked). Either way, you are limited to a single instance or very small cluster and not worth the overhead on testing infra if you want to just stub out some failure cases. tl,dr: Use Simulacron for single or multi-node. One of the others if you want to just do some single-instance crud. – zznate May 30 '19 at 22:18
4

We use Cassandra unit for unit tests. This library provides useful helpers and allows to start Embedded Cassandra from code very easy

EmbeddedCassandraServerHelper.startEmbeddedCassandra();
Mikhail Baksheev
  • 1,394
  • 11
  • 13
0

You can also take a look at https://github.com/nosan/embedded-cassandra/wiki

e.g. If you want to start Cassandra using JUnit4 you can do this in one line:

public class CassandraRuleTests {

    @ClassRule
    public static final CassandraRule cassandra = new CassandraRule(CqlScript.classpath("init.cql"));

    @Test
    public void testMe() {

    }

}

Cassandra will be forked as a separate process. You can also set path to JAVA_HOME 8 via javaHome property in case if you use java 9 and higher.

Dmytro Nosan
  • 161
  • 1
  • 5
0

You will need to provide the below configuration to cassandra-maven-plugin to start native transport port (9042)

<startNativeTransport>true</startNativeTransport>

So your plugin will looke like

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cassandra-maven-plugin</artifactId>
                <version>3.6</version>
                <configuration>
                    <startNativeTransport>true</startNativeTransport>
                </configuration>
            </plugin>
Vivek
  • 11,938
  • 19
  • 92
  • 127