How to use PostgreSQLContainer class of org.testcontainers.containers package

Best Testcontainers-java code snippet using org.testcontainers.containers.PostgreSQLContainer

Source:DaoTestBase.java Github

copy

Full Screen

...14import org.springframework.test.annotation.DirtiesContext;15import org.springframework.test.context.ContextConfiguration;16import org.springframework.test.context.junit4.SpringRunner;17import org.testcontainers.containers.FailureDetectingExternalResource;18import org.testcontainers.containers.PostgreSQLContainer;19import java.util.function.Consumer;20@RunWith(SpringRunner.class)21@ContextConfiguration(initializers = DaoTestBase.Initializer.class)22@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)23@Slf4j24public abstract class DaoTestBase extends AbstractTestUtils {25 private static TestContainers testContainers =26 TestContainersBuilder.builderWithTestContainers(TestContainersParameters::new)27 .addPostgresqlTestContainer()28 .build();29 @ClassRule30 public static final FailureDetectingExternalResource resource = new FailureDetectingExternalResource() {31 @Override32 protected void starting(Description description) {33 testContainers.startTestContainers();34 }35 @Override36 protected void failed(Throwable e, Description description) {37 log.warn("Test Container start failed ", e);38 }39 @Override40 protected void finished(Description description) {41 testContainers.stopTestContainers();42 }43 };44 private static Consumer<EnvironmentProperties> getEnvironmentPropertiesConsumer() {45 return environmentProperties -> {46 PostgreSQLContainer postgreSqlContainer = testContainers.getPostgresqlTestContainer().get();47 environmentProperties.put("spring.datasource.url", postgreSqlContainer.getJdbcUrl());48 environmentProperties.put("spring.datasource.username", postgreSqlContainer.getUsername());49 environmentProperties.put("spring.datasource.password", postgreSqlContainer.getPassword());50 environmentProperties.put("spring.flyway.url", postgreSqlContainer.getJdbcUrl());51 environmentProperties.put("spring.flyway.user", postgreSqlContainer.getUsername());52 environmentProperties.put("spring.flyway.password", postgreSqlContainer.getPassword());53 };54 }55 public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {56 @Override57 public void initialize(ConfigurableApplicationContext configurableApplicationContext) {58 TestPropertyValues59 .of(testContainers.getEnvironmentProperties(getEnvironmentPropertiesConsumer()))60 .applyTo(configurableApplicationContext);...

Full Screen

Full Screen

Source:BaseIntegrationTestContainer.java Github

copy

Full Screen

1package com.TestingTutorials.springboottesting.BaseIntegration;2import org.springframework.test.context.DynamicPropertyRegistry;3import org.springframework.test.context.DynamicPropertySource;4import org.testcontainers.containers.PostgreSQLContainer;5import org.testcontainers.junit.jupiter.Container;6import org.testcontainers.utility.DockerImageName;7public abstract class BaseIntegrationTestContainer {8 /**9 * This is an implementation of singleton's10 * containers pattern for our testContainers.11 * This is particularly important due to the12 * reuse of the docker containers for all integration tests,13 * Basically to avoid redundancy.14 *15 * **/16 static final PostgreSQLContainer postgresqlContainer;17 // Database container from TestContainers would be shared between test classes by adding static18 static {19 postgresqlContainer = new PostgreSQLContainer(DockerImageName.parse("postgres:13.1"))20 .withDatabaseName("test_employee_RestAPI")21 .withPassword("password")22 .withUsername("postgres");23 postgresqlContainer.start();24 }25 // Make the Results of the container available to the Application context using Dynamic properties26 @DynamicPropertySource27 public static void dynamicProperty(DynamicPropertyRegistry registry){28 registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);29 registry.add("spring.datasource.username", postgresqlContainer::getUsername);30 registry.add("spring.datasource.password", postgresqlContainer::getPassword);31 }32}...

Full Screen

Full Screen

Source:ContainersEnvironment.java Github

copy

Full Screen

1package com.example.DimaKrutoi.config;2import com.example.DimaKrutoi.containers.KafkaTestContainer;3import com.example.DimaKrutoi.containers.PostgresTestContainer;4import org.testcontainers.containers.KafkaContainer;5import org.testcontainers.containers.PostgreSQLContainer;6import org.testcontainers.junit.jupiter.Container;7import org.testcontainers.junit.jupiter.Testcontainers;8@Testcontainers9public class ContainersEnvironment {10 @Container11 public static PostgreSQLContainer postgreSQLContainer = PostgresTestContainer.getInstance();12 @Container13 public static KafkaContainer kafkaContainer = KafkaTestContainer.getInstance();14}...

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2public class 1 {3 public static void main(String[] args) {4 PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();5 postgreSQLContainer.start();6 System.out.println("PostgreSQLContainer started with JDBC URL: " + postgreSQLContainer.getJdbcUrl());7 }8}9CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR ( 50 ) NOT NULL, email VARCHAR ( 255 ) NOT NULL UNIQUE, created_on TIMESTAMP NOT NULL );10CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR ( 50 ) NOT NULL, email VARCHAR ( 255 ) NOT NULL UNIQUE, created_on TIMESTAMP NOT NULL );11getDatabaseName() – This method is used to

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2public class PostgreSqlContainer extends PostgreSQLContainer<PostgreSqlContainer> {3 private static final String IMAGE_VERSION = "postgres:11.1";4 private static PostgreSqlContainer container;5 private PostgreSqlContainer() {6 super(IMAGE_VERSION);7 }8 public static PostgreSqlContainer getInstance() {9 if (container == null) {10 container = new PostgreSqlContainer();11 }12 return container;13 }14 public void start() {15 super.start();16 System.setProperty("DB_URL", container.getJdbcUrl());17 System.setProperty("DB_USERNAME", container.getUsername());18 System.setProperty("DB_PASSWORD", container.getPassword());19 }20 public void stop() {21 }22}23import org.junit.jupiter.api.AfterAll;24import org.junit.jupiter.api.BeforeAll;25import org.junit.jupiter.api.Test;26import org.testcontainers.containers.PostgreSQLContainer;27import java.sql.Connection;28import java.sql.DriverManager;29import java.sql.SQLException;30import java.sql.Statement;31import static org.junit.jupiter.api.Assertions.assertEquals;32public class PostgreSqlContainerTest {33 private static Connection connection;34 public static void init() throws SQLException {35 PostgreSQLContainer container = PostgreSqlContainer.getInstance();36 container.start();37 connection = DriverManager.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword());38 try (Statement statement = connection.createStatement()) {39 statement.execute("CREATE TABLE IF NOT EXISTS test (id SERIAL, name VARCHAR(255))");40 }41 }42 public void test() throws SQLException {43 try (Statement statement = connection.createStatement()) {44 statement.execute("INSERT INTO test (name) VALUES ('test')");45 var resultSet = statement.executeQuery("SELECT * FROM test");46 resultSet.next();47 assertEquals("test", resultSet.getString("name"));48 }49 }50 public static void clean() throws SQLException {51 try (Statement statement = connection.createStatement()) {52 statement.execute("DROP TABLE test");53 }54 connection.close();55 }56}

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2public class 1 {3 public static void main(String[] args) {4 PostgreSQLContainer container = new PostgreSQLContainer("postgres:9.6.8");5 container.start();6 System.out.println("Container is started");7 System.out.println("JDBC URL is "+container.getJdbcUrl());8 System.out.println("Username is "+container.getUsername());9 System.out.println("Password is "+container.getPassword());10 container.stop();11 System.out.println("Container is stopped");12 }13}

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2public class 1 {3 public static void main(String[] args) {4 PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();5 postgreSQLContainer.start();6 System.out.println("JDBC URL: " + postgreSQLContainer.getJdbcUrl());7 System.out.println("Username: " + postgreSQLContainer.getUsername());8 System.out.println("Password: " + postgreSQLContainer.getPassword());9 postgreSQLContainer.stop();10 }11}

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.utility.DockerImageName;4public class TestPostgres {5 public static void main(String[] args) {6 PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(DockerImageName.parse("postgres:13.1"))7 .withDatabaseName("test")8 .withUsername("user")9 .withPassword("password")10 .withLogConsumer(new Slf4jLogConsumer(System.out));11 postgres.start();12 System.out.println("PostgreSQL container started");13 System.out.println("Connection URL: " + postgres.getJdbcUrl());14 System.out.println("User name: " + postgres.getUsername());15 System.out.println("Password: " + postgres.getPassword());16 System.out.println("Database name: " + postgres.getDatabaseName());17 System.out.println("Port: " + postgres.getFirstMappedPort());18 postgres.stop();19 }20}

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2public class TestContainer {3 public static void main(String[] args) {4 PostgreSQLContainer postgres = new PostgreSQLContainer();5 postgres.start();6 System.out.println("JDBC URL: " + postgres.getJdbcUrl());7 System.out.println("Username: " + postgres.getUsername());8 System.out.println("Password: " + postgres.getPassword());9 postgres.stop();10 }11}

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1package com.testcontainers;2import org.testcontainers.containers.PostgreSQLContainer;3public class TestContainersDemo {4public static void main(String[] args) {5PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();6postgreSQLContainer.start();7System.out.println(postgreSQLContainer.getJdbcUrl());8System.out.println(postgreSQLContainer.getUsername());9System.out.println(postgreSQLContainer.getPassword());10}11}

Full Screen

Full Screen

PostgreSQLContainer

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2public class PostgreSQLContainerExample {3 public static void main(String[] args) {4 PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:10.5");5 postgres.start();6 String jdbcUrl = postgres.getJdbcUrl();7 String username = postgres.getUsername();8 String password = postgres.getPassword();9 String databaseName = postgres.getDatabaseName();10 String containerId = postgres.getContainerId();11 String hostIpAddress = postgres.getHostIpAddress();12 int mappedPort = postgres.getMappedPort(5432);13 int mappedHostPort = postgres.getMappedPort(5432);14 int exposedHostPort = postgres.getExposedPort(5432);15 String containerIpAddress = postgres.getContainerIpAddress();16 int exposedPort = postgres.getExposedPort(5432);17 int mappedHostPort1 = postgres.getMappedPort(5432);18 int mappedPort1 = postgres.getMappedPort(5432);19 int mappedHostPort2 = postgres.getMappedPort(5432);20 int mappedPort2 = postgres.getMappedPort(5432);21 int mappedHostPort3 = postgres.getMappedPort(5432);22 int mappedPort3 = postgres.getMappedPort(5432);23 int mappedHostPort4 = postgres.getMappedPort(5432);24 int mappedPort4 = postgres.getMappedPort(5432);25 int mappedHostPort5 = postgres.getMappedPort(5432);26 int mappedPort5 = postgres.getMappedPort(5432);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful