Best Testcontainers-java code snippet using org.testcontainers.containers.CassandraContainer.CassandraContainer
Source:CassandraDatabaseInitializer.java
...17import org.springframework.data.cassandra.config.CqlSessionFactoryBean;18import org.springframework.lang.NonNull;19import org.slf4j.Logger;20import org.slf4j.LoggerFactory;21import org.testcontainers.containers.CassandraContainer;22/**23 * Abstract base class using Testcontainers to bootstrap, configure and initialize an Apache Cassandra database.24 *25 * @author John Blum26 * @see org.testcontainers.containers.CassandraContainer27 * @since 1.0.028 */29@SuppressWarnings("unused")30public abstract class CassandraDatabaseInitializer implements Runnable {31 public static final int CASSANDRA_DEFAULT_PORT = CqlSessionFactoryBean.DEFAULT_PORT;32 public static final String CASSANDRA_CONTACT_POINTS_PROPERTY = "spring.app.cassandra.contact-points";33 public static final String CASSANDRA_DEFAULT_HOSTNAME = "localhost";34 public static final String CASSANDRA_LOCAL_DATACENTER = "datacenter1";35 public static final String CASSANDRA_SCHEMA_CQL = "cassandra-schema.cql";36 public static final String CASSANDRA_SYSTEM_KEYSPACE_NAME = "system";37 public static final String CASSANDRA_TEST_KEYSPACE_NAME = "test";38 public static final String CASSANDRA_USERS_TABLE_NAME = "Users";39 protected static final String CASSANDRA_DOCKER_IMAGE_NAME = "cassandra:latest";40 public static CassandraDatabaseInitializer init() {41 CassandraDatabaseInitializer initializer = new CassandraDatabaseInitializer() { };42 initializer.run();43 return initializer;44 }45 private final CassandraContainer<?> cassandraContainer;46 private final Logger logger = LoggerFactory.getLogger(getClass());47 protected CassandraDatabaseInitializer() {48 this.cassandraContainer = newCassandraContainer();49 }50 protected CassandraContainer<?> newCassandraContainer() {51 return new CassandraContainer<>(CASSANDRA_DOCKER_IMAGE_NAME)52 .withExposedPorts(CASSANDRA_DEFAULT_PORT)53 .withInitScript(CASSANDRA_SCHEMA_CQL)54 .withReuse(true);55 }56 public @NonNull CassandraContainer<?> getCassandraContainer() {57 return this.cassandraContainer;58 }59 protected @NonNull Logger getLogger() {60 return this.logger;61 }62 @Override63 public void run() {64 getLogger().info("Bootstrapping Apache Cassandra database...");65 // Run the Apache Cassandra database.66 getCassandraContainer().start();67 configureEnvironment(getCassandraContainer());68 getLogger().info("Apache Cassandra databased initialized!");69 }70 private @NonNull CassandraContainer<?> configureEnvironment(@NonNull CassandraContainer<?> cassandraContainer) {71 String contactPoints = String.format("%s[%d]",72 CASSANDRA_DEFAULT_HOSTNAME, cassandraContainer.getMappedPort(CASSANDRA_DEFAULT_PORT));73 getLogger().info("Apache Cassandra database contact points [{}]", contactPoints);74 System.setProperty(CASSANDRA_CONTACT_POINTS_PROPERTY, contactPoints);75 getLogger().info("System-Property['{}'] = {}", CASSANDRA_CONTACT_POINTS_PROPERTY,76 System.getProperty(CASSANDRA_CONTACT_POINTS_PROPERTY));77 return cassandraContainer;78 }79}...
Source:CassandraContainerInitializer.java
...6import org.slf4j.LoggerFactory;7import org.springframework.boot.test.util.TestPropertyValues;8import org.springframework.context.ApplicationContextInitializer;9import org.springframework.context.ConfigurableApplicationContext;10import org.testcontainers.containers.CassandraContainer;11import org.testcontainers.utility.DockerImageName;12import java.util.List;13import java.util.stream.Collectors;14/**15 * ApplicationContextInitializer implementation for managing the Cassandra Testcontainers container.16 */17public class CassandraContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {18 private static final Logger log = LoggerFactory.getLogger(CassandraContainerInitializer.class);19 private static final String KEYSPACE_NAME = "demo";20 private static final String DATA_CENTER = "datacenter1";21 private static final String SCHEMA_ACTION = "create_if_not_exists";22 private static final int EXPOSED_PORT = 9042;23 private static final String CREATE_KEYSPACE_QUERY =24 String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'SimpleStrategy','replication_factor':'1'};",25 KEYSPACE_NAME);26 private static CassandraContainer cassandraContainer =27 (CassandraContainer) new CassandraContainer(DockerImageName.parse("cassandra:3.11.2"))28 .withExposedPorts(EXPOSED_PORT);29 static {30 log.info("Starting the Cassandra container...");31 cassandraContainer.start();32 }33 public void initialize(ConfigurableApplicationContext configurableApplicationContext) {34 log.info("Initializing Cassandra container...");35 log.info(String.format("Docker image name: %s", cassandraContainer.getDockerImageName()));36 log.info(String.format("Docker container ID: %s", cassandraContainer.getContainerId()));37 log.info(String.format("Docker container name: %s", cassandraContainer.getContainerName()));38 TestPropertyValues.of(39 String.format("spring.data.cassandra.keyspace-name=%s", KEYSPACE_NAME),40 String.format("spring.data.cassandra.contact-points=%s", cassandraContainer.getContainerIpAddress()),41 String.format("spring.data.cassandra.port=%s", cassandraContainer.getMappedPort(EXPOSED_PORT)),...
Source:CassandraLocalContainerService.java
...18import org.apache.camel.test.infra.cassandra.common.CassandraProperties;19import org.apache.camel.test.infra.common.services.ContainerService;20import org.slf4j.Logger;21import org.slf4j.LoggerFactory;22import org.testcontainers.containers.CassandraContainer;23/**24 * A service for a local instance of Apache Cassandra running with TestContainers25 */26public class CassandraLocalContainerService implements CassandraService, ContainerService<CassandraContainer> {27 private static final Logger LOG = LoggerFactory.getLogger(CassandraLocalContainerService.class);28 private static final String IMAGE_NAME = "cassandra:3.11.2";29 private final CassandraContainer container;30 public CassandraLocalContainerService() {31 this(System.getProperty(CassandraProperties.CASSANDRA_CONTAINER, IMAGE_NAME));32 }33 public CassandraLocalContainerService(String imageName) {34 container = initContainer(IMAGE_NAME);35 }36 public CassandraLocalContainerService(CassandraContainer container) {37 this.container = container;38 }39 protected CassandraContainer initContainer(String imageName) {40 return new CassandraContainer(imageName);41 }42 @Override43 public int getCQL3Port() {44 return container.getMappedPort(CassandraContainer.CQL_PORT);45 }46 @Override47 public String getCassandraHost() {48 return container.getHost();49 }50 @Override51 public void registerProperties() {52 System.setProperty(CassandraProperties.CASSANDRA_HOST, getCassandraHost());53 System.setProperty(CassandraProperties.CASSANDRA_CQL3_PORT, String.valueOf(getCQL3Port()));54 }55 @Override56 public void initialize() {57 container.start();58 registerProperties();59 LOG.info("Cassandra server running at address {}", getCQL3Endpoint());60 }61 @Override62 public void shutdown() {63 LOG.info("Stopping the Cassandra container");64 container.stop();65 }66 @Override67 public CassandraContainer getContainer() {68 return container;69 }70}...
CassandraContainer
Using AI Code Generation
1import org.testcontainers.containers.CassandraContainer;2public class 1 {3 public static void main(String[] args) {4 CassandraContainer cassandra = new CassandraContainer("cassandra:latest");5 cassandra.start();6 System.out.println("Cassandra started");7 System.out.println("JMX port: " + cassandra.getJmxPort());8 System.out.println("Native transport port: " + cassandra.getNativeTransportPort());9 System.out.println("Cluster name: " + cassandra.getClusterName());10 System.out.println("CQL port: " + cassandra.getCqlPort());11 System.out.println("Container ID: " + cassandra.getContainerId());12 System.out.println("Container name: " + cassandra.getContainerName());13 System.out.println("Docker image name: " + cassandra.getDockerImageName());14 System.out.println("Host: " + cassandra.getHost());15 System.out.println("Image: " + cassandra.getImage());16 System.out.println("Logs: " + cassandra.getLogs());17 System.out.println("Mapped port: " + cassandra.getMappedPort(9042));18 System.out.println("Network: " + cassandra.getNetwork());19 System.out.println("Password: " + cassandra.getPassword());20 System.out.println("RMI port: " + cassandra.getRmiPort());21 System.out.println("Thrift port: " + cassandra.getThriftPort());22 System.out.println("Username: " + cassandra.getUsername());23 System.out.println("Waiting for log message: " + cassandra.waitingForLogMessage("Created default superuser role 'cassandra'", 60));24 cassandra.stop();25 System.out.println("Cassandra stopped");26 }27}
CassandraContainer
Using AI Code Generation
1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.containers.wait.strategy.WaitStrategy;5import org.testcontainers.utility.DockerImageName;6import org.testcontainers.utility.MountableFile;7import java.util.concurrent.TimeUnit;8public class CassandraContainerDemo {9 public static void main(String[] args) {10 CassandraContainer cassandraContainer = new CassandraContainer(DockerImageName.parse("cassandra:3.11.6"));11 cassandraContainer.start();12 System.out.println("CassandraContainer Started");13 }14}15import org.testcontainers.containers.CassandraContainer;16import org.testcontainers.containers.GenericContainer;17import org.testcontainers.containers.wait.strategy.Wait;18import org.testcontainers.containers.wait.strategy.WaitStrategy;19import org.testcontainers.utility.DockerImageName;20import org.testcontainers.utility.MountableFile;21import java.util.concurrent.TimeUnit;22public class CassandraContainerDemo {23 public static void main(String[] args) {24 GenericContainer cassandraContainer = new GenericContainer(DockerImageName.parse("cassandra:3.11.6"));25 cassandraContainer.start();26 System.out.println("CassandraContainer Started");27 }28}29import org.testcontainers.containers.CassandraContainer;30import org.testcontainers.containers.GenericContainer;31import org.testcontainers.containers.wait.strategy.Wait;32import org.testcontainers.containers.wait.strategy.WaitStrategy;33import org.testcontainers.utility.DockerImageName;34import org.testcontainers.utility.MountableFile;35import java.util.concurrent.TimeUnit;36public class CassandraContainerDemo {37 public static void main(String[] args) {38 GenericContainer cassandraContainer = new GenericContainer(DockerImageName.parse("cassandra:3.11.6"))39 .withExposedPorts(9042)40 .withEnv("CASSANDRA_CLUSTER_NAME", "Test Cluster")41 .withEnv("CASSANDRA_ENDPOINT_SNITCH", "GossipingPropertyFileSnitch")42 .withEnv("MAX_HEAP_SIZE", "512M")
CassandraContainer
Using AI Code Generation
1import org.testcontainers.containers.CassandraContainer;2public class 1{3 public static void main(String[] args) {4 CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.3");5 cassandra.start();6 System.out.println("Cassandra started");7 }8}
CassandraContainer
Using AI Code Generation
1import org.testcontainers.containers.CassandraContainer;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.utility.DockerImageName;5public class CassandraContainerTest {6 public static void main(String[] args) {7 CassandraContainer cassandraContainer = new CassandraContainer();8 cassandraContainer.start();9 System.out.println("CassandraContainer started");10 System.out.println("Container ID: " + cassandraContainer.getContainerId());11 System.out.println("Host: " + cassandraContainer.getHost());12 System.out.println("Port: " + cassandraContainer.getFirstMappedPort());13 System.out.println("Cluster: " + cassandraContainer.getCluster());14 System.out.println("Keyspace: " + cassandraContainer.getKeyspace());15 System.out.println("Username: " + cassandraContainer.getUsername());16 System.out.println("Password: " + cassandraContainer.getPassword());17 System.out.println("JMX port: " + cassandraContainer.getJmxPort());18 System.out.println("JMX host: " + cassandraContainer.getJmxHost());19 System.out.println("CQL port: " + cassandraContainer.getCqlPort());20 System.out.println("Thrift port: " + cassandraContainer.getThriftPort());21 System.out.println("Native transport port: " + cassandraContainer.getNativeTransportPort());22 System.out.println("Storage port: " + cassandraContainer.getStoragePort());23 System.out.println("SSL storage port: " + cassandraContainer.getSslStoragePort());24 System.out.println("Docker image: " + cassandraContainer.getDockerImageName());25 System.out.println("Cassandra version: " + cassandraContainer.getCassandraVersion());26 System.out.println("Cassandra major version: " + cassandraContainer.getCassandraMajorVersion());27 System.out.println("Cassandra minor version: " + cassandraContainer.getCassandraMinorVersion());28 System.out.println("Cassandra patch vers
CassandraContainer
Using AI Code Generation
1package org.testcontainers;2import org.testcontainers.containers.CassandraContainer;3public class CassandraContainerTest {4 public static void main(String[] args) {5 try (CassandraContainer cassandra = new CassandraContainer("cassandra:3.11.3")) {6 cassandra.start();7 System.out.println("Cassandra started");8 }9 }10}11Java | CassandraContainer Class getCluster()12Java | CassandraContainer Class getClusterBuilder()13Java | CassandraContainer Class getCassandraVersion()14Java | CassandraContainer Class getContainerIpAddress()15Java | CassandraContainer Class getExposedPorts()16Java | CassandraContainer Class getHost()17Java | CassandraContainer Class getMappedPort()18Java | CassandraContainer Class getPortBindings()19Java | CassandraContainer Class getSeedHosts()20Java | CassandraContainer Class getThriftPort()21Java | CassandraContainer Class getTransportPort()22Java | CassandraContainer Class withEnv()23Java | CassandraContainer Class withExposedPorts()24Java | CassandraContainer Class withLogConsumer()25Java | CassandraContainer Class withNetwork()26Java | CassandraContainer Class withNetworkAliases()
CassandraContainer
Using AI Code Generation
1import org.testcontainers.containers.CassandraContainer;2public class 1 {3 public static void main(String[] args) {4 CassandraContainer cassandra = new CassandraContainer();5 cassandra.start();6 System.out.println("Cassandra Started");7 }8}9import org.testcontainers.containers.CassandraContainer;10public class 2 {11 public static void main(String[] args) {12 CassandraContainer cassandra = new CassandraContainer("cassandra:3.11");13 cassandra.start();14 System.out.println("Cassandra Started");15 }16}17import org.testcontainers.containers.CassandraContainer;18public class 3 {19 public static void main(String[] args) {20 CassandraContainer cassandra = new CassandraContainer("cassandra:3.11")21 .withExposedPorts(9042)22 .withEnv("CASSANDRA_CLUSTER_NAME", "Test Cluster")23 .withEnv("MAX_HEAP_SIZE", "512M")24 .withEnv("HEAP_NEWSIZE", "100M")25 .withCommand("--debug");26 cassandra.start();27 System.out.println("Cassandra Started");28 }29}30import org.testcontainers.containers.CassandraContainer;31public class 4 {32 public static void main(String[] args) {33 CassandraContainer cassandra = new CassandraContainer("cassandra:3.11")34 .withExposedPorts(9042)35 .withEnv("CASSANDRA_CLUSTER_NAME", "Test Cluster")36 .withEnv("MAX_HEAP_SIZE", "512M")37 .withEnv("HEAP_NEWSIZE", "100M")38 .withCommand("--debug");39 cassandra.start();40 System.out.println("Cassandra Started");41 }42}43import org.testcontainers.containers.CassandraContainer
CassandraContainer
Using AI Code Generation
1package com.testcontainers;2import java.io.IOException;3import java.net.InetSocketAddress;4import java.net.Socket;5import java.net.SocketAddress;6import java.util.concurrent.TimeUnit;7import org.junit.Assert;8import org.junit.Test;9import org.testcontainers.containers.CassandraContainer;10public class CassandraTest {11 public void testCassandraContainer() throws IOException, InterruptedException {12 CassandraContainer cassandraContainer = new CassandraContainer();13 cassandraContainer.start();14 System.out.println(cassandraContainer.getCluster().getMetadata().getClusterName());15 System.out.println(cassandraContainer.getCluster().getMetadata().getAllHosts());16 SocketAddress socketAddress = new InetSocketAddress(cassandraContainer.getContainerIpAddress(), cassandraContainer.getMappedPort(9042));17 Socket socket = new Socket();18 socket.connect(socketAddress, (int) TimeUnit.SECONDS.toMillis(5));19 socket.close();20 Assert.assertTrue(socket.isConnected());21 cassandraContainer.stop();22 }23}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!