Best Testcontainers-java code snippet using org.testcontainers.containers.CockroachContainer
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);...
Source:CamundaCockroachDBContainerProvider.java
...14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17package org.camunda.impl.test.utils.testcontainers;18import org.testcontainers.containers.CockroachContainer;19import org.testcontainers.containers.CockroachContainerProvider;20import org.testcontainers.containers.JdbcDatabaseContainer;21import org.testcontainers.utility.DockerImageName;22public class CamundaCockroachDBContainerProvider extends CockroachContainerProvider {23 private static final String NAME = "camcockroachdb";24 @Override25 public boolean supports(String databaseType) {26 return NAME.equals(databaseType);27 }28 @Override29 public JdbcDatabaseContainer newInstance(String tag) {30 DockerImageName dockerImageName = TestcontainersHelper31 .resolveDockerImageName("cockroachdb", tag, "cockroachdb/cockroach");32 return new CockroachContainer(dockerImageName);33 }34}...
Source:AppTest.java
1package mcs;2import static org.junit.jupiter.api.Assertions.assertTrue;3import org.junit.jupiter.api.Assertions;4import org.junit.jupiter.api.Test;5import org.testcontainers.containers.GenericContainer;6import org.testcontainers.elasticsearch.ElasticsearchContainer;7import org.testcontainers.junit.jupiter.Container;8import org.testcontainers.junit.jupiter.Testcontainers;9import org.testcontainers.utility.DockerImageName;10@Testcontainers11public class AppTest {12 @Container13 private static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(14 DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch").withTag("7.11.1")15 );16 @Container 17 private static final GenericContainer<?> cockroachContainer = new GenericContainer<>(18 DockerImageName.parse("cockroachdb/cockroach")19 .withTag("latest")20 )21 .withExposedPorts(26257)22 .withCommand("start-single-node --insecure");23 @Test24 public void elasticHealth() {25 assertTrue(elasticsearch.isRunning());26 int mappedPort = elasticsearch.getMappedPort(9200);27 App app = new App();28 Assertions.assertDoesNotThrow(() -> {29 app.elastic(mappedPort);30 });31 }32 @Test33 public void cockroachHealth() {34 assertTrue(cockroachContainer.isRunning());35 }36}...
CockroachContainer
Using AI Code Generation
1import org.testcontainers.containers.CockroachContainer;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.containers.output.ToStringConsumer;5import org.testcontainers.containers.wait.strategy.Wait;6import org.testcontainers.containers.wait.strategy.WaitAllStrategy;7import org.testcontainers.containers.wait.strategy.WaitStrategy;8import org.testcontainers.utility.DockerImageName;9import java.sql.Connection;10import java.sql.DriverManager;11import java.sql.SQLException;12import java.sql.Statement;13import java.util.concurrent.TimeUnit;14public class CockroachDBContainer extends CockroachContainer<CockroachDBContainer> {15 private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("cockroachdb/cockroach");16 private static final String DEFAULT_TAG = "v20.2.4";17 private static final String DEFAULT_IMAGE = DEFAULT_IMAGE_NAME.getUnversionedPart() + ":" + DEFAULT_TAG;18 public CockroachDBContainer() {19 this(DEFAULT_IMAGE);20 }21 public CockroachDBContainer(final String dockerImageName) {22 super(dockerImageName);23 }24 protected void configure() {25 super.configure();26 withCommand("start-single-node", "--insecure");27 withEnv("COCKROACH_INSECURE", "true");28 withEnv("COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING", "true");29 withEnv("COCKROACH_DATABASE", "testdb");30 withEnv("COCKROACH_USER", "testuser");31 withEnv("COCKROACH_PASSWORD", "testpassword");32 withExposedPorts(26257, 8080);33 waitingFor(new WaitAllStrategy().withStrategy(Wait.forHttp("/health").forStatusCode(200).forPort(8080))34 .withStrategy(Wait.forListeningPort()));35 }36 public Connection createConnection() throws SQLException {37 getMappedPort(26257), getUsername(), getPassword());38 return DriverManager.getConnection(jdbcUrl);39 }40 public String getUsername() {41 return "testuser";42 }43 public String getPassword() {44 return "testpassword";45 }46 public String getDatabaseName() {47 return "testdb";48 }49 public static void main(String[] args) {
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!!