Best Testcontainers-java code snippet using org.testcontainers.containers.CockroachContainer.CockroachContainer
Source:CockroachContainer.java
1package org.testcontainers.containers;2import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;3import org.testcontainers.utility.DockerImageName;4import java.time.Duration;5public class CockroachContainer extends JdbcDatabaseContainer<CockroachContainer> {6 private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("cockroachdb/cockroach");7 private static final String DEFAULT_TAG = "v19.2.11";8 public static final String NAME = "cockroach";9 @Deprecated10 public static final String IMAGE = DEFAULT_IMAGE_NAME.getUnversionedPart();11 @Deprecated12 public static final String IMAGE_TAG = DEFAULT_TAG;13 private static final String JDBC_DRIVER_CLASS_NAME = "org.postgresql.Driver";14 private static final String JDBC_URL_PREFIX = "jdbc:postgresql";15 private static final String TEST_QUERY_STRING = "SELECT 1";16 private static final int REST_API_PORT = 8080;17 private static final int DB_PORT = 26257;18 private String databaseName = "postgres";19 private String username = "root";20 private String password = "";21 /**22 * @deprecated use {@link CockroachContainer(DockerImageName)} instead23 */24 @Deprecated25 public CockroachContainer() {26 this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));27 }28 public CockroachContainer(final String dockerImageName) {29 this(DockerImageName.parse(dockerImageName));30 }31 public CockroachContainer(final DockerImageName dockerImageName) {32 super(dockerImageName);33 dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);34 withExposedPorts(REST_API_PORT, DB_PORT);35 waitingFor(36 new HttpWaitStrategy()37 .forPath("/health")38 .forPort(REST_API_PORT)39 .forStatusCode(200)40 .withStartupTimeout(Duration.ofMinutes(1))41 );42 withCommand("start-single-node --insecure");43 }44 @Override45 public String getDriverClassName() {46 return JDBC_DRIVER_CLASS_NAME;47 }48 @Override49 public String getJdbcUrl() {50 String additionalUrlParams = constructUrlParameters("?", "&");51 return JDBC_URL_PREFIX + "://" + getHost() + ":" + getMappedPort(DB_PORT) +52 "/" + databaseName + additionalUrlParams;53 }54 @Override55 public final String getDatabaseName() {56 return databaseName;57 }58 @Override59 public String getUsername() {60 return username;61 }62 @Override63 public String getPassword() {64 return password;65 }66 @Override67 public String getTestQueryString() {68 return TEST_QUERY_STRING;69 }70 @Override71 public CockroachContainer withUsername(String username) {72 throw new UnsupportedOperationException("The CockroachDB docker image does not currently support this - please see https://github.com/cockroachdb/cockroach/issues/19826");73 }74 @Override75 public CockroachContainer withPassword(String password) {76 throw new UnsupportedOperationException("The CockroachDB docker image does not currently support this - please see https://github.com/cockroachdb/cockroach/issues/19826");77 }78 @Override79 public CockroachContainer withDatabaseName(final String databaseName) {80 throw new UnsupportedOperationException("The CockroachDB docker image does not currently support this - please see https://github.com/cockroachdb/cockroach/issues/19826");81 }82}...
Source:SimpleCockroachDBTest.java
1package org.testcontainers.junit.cockroachdb;2import org.junit.Test;3import org.testcontainers.containers.CockroachContainer;4import org.testcontainers.db.AbstractContainerDatabaseTest;5import java.sql.ResultSet;6import java.sql.SQLException;7import java.util.logging.Level;8import java.util.logging.LogManager;9import static org.hamcrest.CoreMatchers.containsString;10import static org.junit.Assert.assertThat;11import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;12import static org.testcontainers.CockroachDBTestImages.COCKROACHDB_IMAGE;13public class SimpleCockroachDBTest extends AbstractContainerDatabaseTest {14 static {15 // Postgres JDBC driver uses JUL; disable it to avoid annoying, irrelevant, stderr logs during connection testing16 LogManager.getLogManager().getLogger("").setLevel(Level.OFF);17 }18 @Test19 public void testSimple() throws SQLException {20 try (CockroachContainer cockroach = new CockroachContainer(COCKROACHDB_IMAGE)) {21 cockroach.start();22 ResultSet resultSet = performQuery(cockroach, "SELECT 1");23 int resultSetInt = resultSet.getInt(1);24 assertEquals("A basic SELECT query succeeds", 1, resultSetInt);25 }26 }27 @Test28 public void testExplicitInitScript() throws SQLException {29 try (CockroachContainer cockroach = new CockroachContainer(COCKROACHDB_IMAGE)30 .withInitScript("somepath/init_postgresql.sql")) { // CockroachDB is expected to be compatible with Postgres31 cockroach.start();32 ResultSet resultSet = performQuery(cockroach, "SELECT foo FROM bar");33 String firstColumnValue = resultSet.getString(1);34 assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);35 }36 }37 @Test38 public void testWithAdditionalUrlParamInJdbcUrl() {39 CockroachContainer cockroach = new CockroachContainer(COCKROACHDB_IMAGE)40 .withUrlParam("sslmode", "disable")41 .withUrlParam("application_name", "cockroach");42 try {43 cockroach.start();44 String jdbcUrl = cockroach.getJdbcUrl();45 assertThat(jdbcUrl, containsString("?"));46 assertThat(jdbcUrl, containsString("&"));47 assertThat(jdbcUrl, containsString("sslmode=disable"));48 assertThat(jdbcUrl, containsString("application_name=cockroach"));49 } finally {50 cockroach.stop();51 }52 }53}...
Source:AbstractCockroachDbTest.java
...9import org.springframework.test.context.ContextConfiguration;10import org.springframework.test.context.DynamicPropertyRegistry;11import org.springframework.test.context.DynamicPropertySource;12import org.springframework.test.context.junit4.SpringRunner;13import org.testcontainers.containers.CockroachContainer;14import org.testcontainers.containers.wait.strategy.Wait;15import org.testcontainers.junit.jupiter.Testcontainers;16// Useful examples for:17// Flyway:18// * https://gist.github.com/coderatchet/cfa73134bd4bbb9b3acfe0dab7fb82bb19@RunWith(SpringRunner.class)20@TestInstance(TestInstance.Lifecycle.PER_CLASS)21@ContextConfiguration(classes = { CockroachDbTestDataConfiguration.class, FlywayAutoConfiguration.class })22@Testcontainers23public abstract class AbstractCockroachDbTest {24 private static final String DB_USERNAME = "root";25 private static final String DB_PASSWORD = "";26 // Needs to be static so that it runs first before all the automatic27 // configurations are run.28 @ClassRule29 public static CockroachContainer cockroachDbContainer = new CockroachContainer("cockroachdb/cockroach:v21.2.3")30 .waitingFor(Wait.forHttp("/").forStatusCode(200))31 .withLogConsumer(outputFrame -> System.out.println("LOG: " + outputFrame.getUtf8String()));32 @BeforeClass33 public static void setUp() throws Exception {34 Flyway flyway = getFlyway();35 flyway.clean();36 flyway.baseline();37 flyway.migrate();38 }39 @DynamicPropertySource40 static void registerPgProperties(DynamicPropertyRegistry registry) {41 registry.add("spring.datasource.url", () -> cockroachDbContainer.getJdbcUrl());42 registry.add("spring.datasource.username", () -> DB_USERNAME);43 registry.add("spring.datasource.password", () -> DB_PASSWORD);...
CockroachContainer
Using AI Code Generation
11 import org.testcontainers.containers.CockroachContainer;22 import org.testcontainers.containers.GenericContainer;33 import org.testcontainers.containers.PostgreSQLContainer;44 import org.testcontainers.containers.output.Slf4jLogConsumer;55 import org.testcontainers.containers.output.WaitingConsumer;66 import org.testcontainers.containers.output.OutputFrame;77 import org.testcontainers.containers.output.FrameConsumerResultCallback;88 import org.testcontainers.containers.output.OutputFrame.OutputType;99 import org.testcontainers.containers.output.ToStringConsumer;1010 import org.testcontainers.containers.output.BaseConsumer;1111 import org.testcontainers.containers.output.FrameConsumerResultCallback;1212 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResult;1313 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;1414 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;1515 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;1616 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;1717 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;1818 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;1919 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2020 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2121 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2222 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2323 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2424 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2525 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2626 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2727 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2828 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;2929 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;3030 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;3131 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;3232 import org.testcontainers.containers.output.FrameConsumerResultCallback.FrameConsumerResultConsumer;3333 import org.testcontainers.containers.output.FrameConsumerResult
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!!