Best Testcontainers-java code snippet using org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest.createContainer
Source:PostgreSQLR2DBCDatabaseContainerTest.java
...3import org.testcontainers.PostgreSQLTestImages;4import org.testcontainers.r2dbc.AbstractR2DBCDatabaseContainerTest;5public class PostgreSQLR2DBCDatabaseContainerTest extends AbstractR2DBCDatabaseContainerTest<PostgreSQLContainer<?>> {6 @Override7 protected PostgreSQLContainer<?> createContainer() {8 return new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE);9 }10 @Override11 protected ConnectionFactoryOptions getOptions(PostgreSQLContainer<?> container) {12 // get_options {13 ConnectionFactoryOptions options = PostgreSQLR2DBCDatabaseContainer.getOptions(14 container15 );16 // }17 return options;18 }19 protected String createR2DBCUrl() {20 return "r2dbc:tc:postgresql:///db?TC_IMAGE_TAG=10-alpine";21 }...
createContainer
Using AI Code Generation
1 PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres:13.2-alpine");2 container.start();3 ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()4 .option(DRIVER, "postgresql")5 .option(HOST, container.getHost())6 .option(PORT, container.getFirstMappedPort())7 .option(USER, container.getUsername())8 .option(PASSWORD, container.getPassword())9 .option(DATABASE, container.getDatabaseName())10 .build());11 Connection connection = connectionFactory.create().block();12 connection.createStatement("SELECT 1").execute().blockLast();13 connection.close();14 container.stop();15}16The test is not very interesting because it does not use R2DBC. However, it shows how to use the createContainer() method of the
createContainer
Using AI Code Generation
1PostgreSQLR2DBCDatabaseContainer<?> container = PostgreSQLR2DBCDatabaseContainer.createContainer();2container.start();3ConnectionFactory connectionFactory = new ConnectionFactoryOptions()4 .option(DRIVER, "postgresql")5 .option(HOST, container.getHost())6 .option(PORT, container.getFirstMappedPort())7 .option(USER, container.getUsername())8 .option(PASSWORD, container.getPassword())9 .option(DATABASE, container.getDatabaseName())10 .option(SSL_MODE, DISABLE)11 .createConnectionFactory();12R2dbc r2dbc = new R2dbc(connectionFactory);13container.stop();14public class PostgreSQLR2DBCDatabaseContainerTest extends PostgreSQLContainerTest {15 public PostgreSQLR2DBCDatabaseContainer<?> createContainer() {16 return new PostgreSQLR2DBCDatabaseContainer<>(IMAGE);17 }18}19public class PostgreSQLR2DBCDatabaseContainer<SELF extends PostgreSQLR2DBCDatabaseContainer<SELF>> extends PostgreSQLContainer<SELF> {20 public PostgreSQLR2DBCDatabaseContainer(final String dockerImageName) {21 super(dockerImageName);22 }23 protected String createConnectionUrl() {24 }25}26public class PostgreSQLR2DBCDatabaseContainerTest extends PostgreSQLContainerTest {27 public PostgreSQLR2DBCDatabaseContainer<?> createContainer() {28 return new PostgreSQLR2DBCDatabaseContainer<>(IMAGE);29 }30}31public class PostgreSQLR2DBCDatabaseContainer<SELF extends PostgreSQLR2DBCDatabaseContainer<SELF>> extends PostgreSQLContainer<SELF> {
createContainer
Using AI Code Generation
1import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainer2import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest3import org.testcontainers.junit.jupiter.Container4import org.testcontainers.junit.jupiter.Testcontainers5import org.junit.jupiter.api.Test6import org.junit.jupiter.api.Assertions.*7import org.springframework.r2dbc.core.DatabaseClient8import org.springframework.r2dbc.core.awaitSingle9class R2DBCPostgreSQLContainerTest {10 val container = PostgreSQLR2DBCDatabaseContainerTest.createContainer("test", "test", "test")11 fun test() {12 val client = DatabaseClient.create(container.createConnectionFactory())13 val result = client.execute("SELECT 1").fetch().awaitSingle()14 assertEquals(1, result.get(0))15 }16}17import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainer18val container = PostgreSQLR2DBCDatabaseContainer("postgres:12")
createContainer
Using AI Code Generation
1public void testCustomDriver() {2 PostgreSQLR2DBCDatabaseContainer container = new PostgreSQLR2DBCDatabaseContainer("postgres:11.5")3 .withDatabaseName("test")4 .withUsername("sa")5 .withPassword("sa");6 container.start();7 R2dbcDriverConfiguration driverConfiguration = R2dbcDriverConfiguration.builder()8 .url(container.getJdbcUrl())9 .username(container.getUsername())10 .password(container.getPassword())11 .build();12 ConnectionFactory connectionFactory = ConnectionFactories.get(driverConfiguration);13 Connection connection = connectionFactory.create().block();14 Statement statement = connection.createStatement("SELECT 1");15 Flux<Integer> result = statement.execute()16 .flatMap(it -> it.map((row, rowMetadata) -> row.get(0, Integer.class)));17 StepVerifier.create(result)18 .expectNext(1)19 .verifyComplete();20 connection.close().block();21 container.stop();22}23The following is the output of the testCustomDriver() method:
createContainer
Using AI Code Generation
1import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest2import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest.createContainer3import org.junit.jupiter.api.Test4import org.junit.jupiter.api.Assertions.assertEquals5class TestPostgreSQLR2DBCDatabaseContainerTest {6 fun testPostgreSQLR2DBCDatabaseContainerTest() {7 val container = createContainer()8 assertEquals("postgres:13.4", container.getDockerImageName())9 assertEquals("postgres", container.getUsername())10 assertEquals("postgres", container.getPassword())11 assertEquals(5432, container.getFirstMappedPort())12 }13}14import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest15import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest.createContainer16import org.junit.jupiter.api.Test17import org.junit.jupiter.api.Assertions.assertEquals18class TestPostgreSQLR2DBCDatabaseContainerTest {19 fun testPostgreSQLR2DBCDatabaseContainerTest() {20 val container = createContainer()21 container.start()22 assertEquals("postgres:13.4", container.getDockerImageName())23 assertEquals("postgres", container.getUsername())24 assertEquals("postgres", container.getPassword())25 assertEquals(5432, container.getFirstMappedPort())26 container.stop()27 }28}29import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest30import org.testcontainers.containers.PostgreSQLR2DBCDatabaseContainerTest.createContainer31import org.junit.jupiter.api
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!!