Testcontainers implementation for Apache Cassandra, providing lightweight, throwaway database instances for Java integration testing
Core functionality for creating, configuring, and managing Cassandra Docker containers with proper lifecycle management and resource cleanup.
Main container class that provides specialized Cassandra container management extending the base GenericContainer functionality.
/**
* Testcontainers implementation for Apache Cassandra.
* Supported image: cassandra
* Exposed ports: 9042
*/
public class CassandraContainer extends GenericContainer<CassandraContainer> {
/**
* Create Cassandra container with Docker image name string
* @param dockerImageName Docker image name (e.g., "cassandra:3.11.2")
*/
public CassandraContainer(String dockerImageName);
/**
* Create Cassandra container with DockerImageName object
* @param dockerImageName DockerImageName instance for version management
*/
public CassandraContainer(DockerImageName dockerImageName);
}Usage Examples:
import org.testcontainers.cassandra.CassandraContainer;
import org.testcontainers.utility.DockerImageName;
// Simple container creation with string
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2");
// Container creation with DockerImageName for version management
DockerImageName cassandraImage = DockerImageName.parse("cassandra").withTag("3.11.2");
CassandraContainer cassandra = new CassandraContainer(cassandraImage);
// Use in try-with-resources for automatic cleanup
try (CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")) {
cassandra.start();
// Perform tests
} // Container automatically stopped and cleaned upThe current implementation keeps the CQL port as a private constant. For accessing the port, use the inherited getMappedPort() method from GenericContainer.
// Access the mapped CQL port
int mappedPort = cassandra.getMappedPort(9042);Note: The deprecated org.testcontainers.containers.CassandraContainer class exposes CQL_PORT as a public constant, but the current implementation does not.
The container extends GenericContainer and inherits all standard Testcontainers lifecycle methods:
start(): Starts the container and waits for it to be readystop(): Stops and removes the containerclose(): Alias for stop(), enabling try-with-resources usageisRunning(): Check if container is currently runningContainer Startup Process:
Automatic Resource Management:
// Preferred pattern - automatic cleanup
try (CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")) {
cassandra.start();
// Container automatically stopped when leaving try block
}
// Manual management (not recommended)
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2");
cassandra.start();
try {
// Perform tests
} finally {
cassandra.stop(); // Manual cleanup required
}The container management system throws specific exceptions for different failure scenarios:
ContainerLaunchException: Thrown when container fails to start or configure properlyIllegalArgumentException: Thrown for invalid Docker image names or incompatible imagesRuntimeException: General container management errorstry (CassandraContainer cassandra = new CassandraContainer("invalid-image")) {
cassandra.start();
} catch (ContainerLaunchException e) {
// Handle container startup failure
logger.error("Failed to start Cassandra container", e);
}The legacy container class in the org.testcontainers.containers package is deprecated but still functional.
import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.utility.DockerImageName;
import com.datastax.driver.core.Cluster;
import java.net.InetSocketAddress;
/**
* @deprecated use org.testcontainers.cassandra.CassandraContainer instead
*/
@Deprecated
public class CassandraContainer<SELF extends CassandraContainer<SELF>>
extends GenericContainer<SELF> {
// Public constants available in deprecated class
public static final Integer CQL_PORT = 9042;
@Deprecated
public static final String IMAGE = "cassandra";
/**
* @deprecated use CassandraContainer(DockerImageName) instead
*/
@Deprecated
public CassandraContainer();
public CassandraContainer(String dockerImageName);
public CassandraContainer(DockerImageName dockerImageName);
// Configuration methods
public SELF withConfigurationOverride(String configLocation);
public SELF withInitScript(String initScriptPath);
public SELF withJmxReporting(boolean enableJmxReporting);
// Connection methods
public String getUsername();
public String getPassword();
public InetSocketAddress getContactPoint();
public String getLocalDatacenter();
/**
* @deprecated For Cassandra driver 3.x, use getHost() and getMappedPort(int) with
* the driver's Cluster.Builder addContactPoint(String) and withPort(int) methods.
* For Cassandra driver 4.x, use getContactPoint() and getLocalDatacenter() with
* the driver's CqlSession.builder() methods.
*/
@Deprecated
public Cluster getCluster();
@Deprecated
public static Cluster getCluster(ContainerState containerState, boolean enableJmxReporting);
@Deprecated
public static Cluster getCluster(ContainerState containerState);
}The deprecated class includes additional JMX reporting configuration not available in the current implementation:
// Enable JMX reporting (deprecated class only)
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withJmxReporting(true);
// Access DataStax driver 3.x Cluster with JMX enabled
Cluster cluster = cassandra.getCluster(); // JMX controlled by withJmxReporting() settingInstall with Tessl CLI
npx tessl i tessl/maven-org-testcontainers--cassandra