Testcontainers implementation for Apache Cassandra, providing lightweight, throwaway database instances for Java integration testing
Configuration override capabilities and CQL script initialization for custom Cassandra setups and test data preparation.
Replace the default Cassandra configuration with custom configuration files from the classpath.
/**
* Initialize Cassandra with custom overridden Cassandra configuration.
* Docker effectively replaces all /etc/cassandra content with the content of config location,
* so if cassandra.yaml in configLocation is absent or corrupted, Cassandra won't launch.
*
* @param configLocation relative classpath with the directory that contains cassandra.yaml
* and other configuration files
* @return CassandraContainer instance for method chaining
*/
public CassandraContainer withConfigurationOverride(String configLocation);Usage Examples:
import org.testcontainers.cassandra.CassandraContainer;
// Use custom configuration from classpath
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withConfigurationOverride("my-cassandra-config");
// Configuration directory structure in src/test/resources:
// my-cassandra-config/
// ├── cassandra.yaml (required - main configuration)
// ├── cassandra-rackdc.properties
// └── logback.xml
cassandra.start();Configuration Requirements:
cassandra.yaml file/etc/cassandra contentcassandra.yaml will cause container startup failureCommon Configuration Use Cases:
// Enable authentication
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withConfigurationOverride("cassandra-auth-config");
// Custom cluster settings
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withConfigurationOverride("cassandra-cluster-config");
// Performance tuning for tests
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withConfigurationOverride("cassandra-test-optimized");Execute CQL scripts after container startup to prepare test data and database schema.
/**
* Initialize Cassandra with init CQL script.
* CQL script will be applied after container is started (using WaitStrategy).
*
* @param initScriptPath relative classpath resource path to CQL script
* @return CassandraContainer instance for method chaining
*/
public CassandraContainer withInitScript(String initScriptPath);Usage Examples:
import org.testcontainers.cassandra.CassandraContainer;
// Execute initialization script from classpath
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withInitScript("init-schema.cql");
// Chain with configuration override
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withConfigurationOverride("cassandra-auth-config")
.withInitScript("init-with-auth.cql");Example Initialization Script (init-schema.cql):
-- Create keyspace
CREATE KEYSPACE IF NOT EXISTS test_keyspace
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
-- Use keyspace
USE test_keyspace;
-- Create table
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username text,
email text,
created_at timestamp
);
-- Insert test data
INSERT INTO users (id, username, email, created_at)
VALUES (uuid(), 'testuser', 'test@example.com', toTimestamp(now()));Script Execution Process:
cqlsh command inside the containerError Handling:
import org.testcontainers.ext.ScriptUtils.ScriptLoadException;
import org.testcontainers.ext.ScriptUtils.UncategorizedScriptException;
try (CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withInitScript("nonexistent-script.cql")) {
cassandra.start();
} catch (ScriptLoadException e) {
// Script file not found in classpath
logger.error("Init script not found", e);
} catch (UncategorizedScriptException e) {
// CQL execution error
logger.error("Init script execution failed", e);
}Common pattern combining custom configuration with initialization scripts for complex test setups.
Usage Example:
// Complete setup with authentication and test data
CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.2")
.withConfigurationOverride("cassandra-auth-enabled")
.withInitScript("setup-auth-and-data.cql");
cassandra.start();
// Connect with authentication
CqlSession session = CqlSession.builder()
.addContactPoint(cassandra.getContactPoint())
.withLocalDatacenter(cassandra.getLocalDatacenter())
.withAuthCredentials(cassandra.getUsername(), cassandra.getPassword())
.build();Execution Order:
ContainerLaunchException: Thrown when configuration directory is empty or cassandra.yaml is invalidIllegalArgumentException: Thrown when configuration path cannot be resolved from classpathScriptLoadException: Thrown when script file cannot be found in classpathScriptStatementFailedException: Thrown when CQL statement execution failsUncategorizedScriptException: Thrown for general script execution errorsInstall with Tessl CLI
npx tessl i tessl/maven-org-testcontainers--cassandra