CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-testcontainers--cassandra

Testcontainers implementation for Apache Cassandra, providing lightweight, throwaway database instances for Java integration testing

Overview
Eval results
Files

container-management.mddocs/

Container Management

Core functionality for creating, configuring, and managing Cassandra Docker containers with proper lifecycle management and resource cleanup.

Capabilities

CassandraContainer Class

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 up

Container Constants

The 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.

Lifecycle Management

The container extends GenericContainer and inherits all standard Testcontainers lifecycle methods:

  • start(): Starts the container and waits for it to be ready
  • stop(): Stops and removes the container
  • close(): Alias for stop(), enabling try-with-resources usage
  • isRunning(): Check if container is currently running

Container Startup Process:

  1. Creates Docker container with Cassandra image
  2. Configures environment variables for optimal testing
  3. Exposes port 9042 for CQL connections
  4. Applies custom wait strategy to ensure Cassandra readiness
  5. Executes configuration overrides if specified
  6. Runs initialization scripts after container is ready

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
}

Error Handling

The container management system throws specific exceptions for different failure scenarios:

  • ContainerLaunchException: Thrown when container fails to start or configure properly
  • IllegalArgumentException: Thrown for invalid Docker image names or incompatible images
  • RuntimeException: General container management errors
try (CassandraContainer cassandra = new CassandraContainer("invalid-image")) {
    cassandra.start();
} catch (ContainerLaunchException e) {
    // Handle container startup failure
    logger.error("Failed to start Cassandra container", e);
}

Deprecated Container Class

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);
}

JMX Reporting Configuration

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() setting

Install with Tessl CLI

npx tessl i tessl/maven-org-testcontainers--cassandra

docs

configuration-initialization.md

connection-access.md

container-management.md

index.md

wait-strategies-delegation.md

tile.json