Populate a database with TestContainers in a SpringBoot integration test

The easiest way is to use JdbcDatabaseContainer::withInitScript Advantage of this solution is that script is run before Spring Application Context loads (at least when it is in a static block) and the code is quite simple. Example: static { postgreSQLContainer = new PostgreSQLContainer(“postgres:9.6.8”) .withDatabaseName(“integration-tests-db”) .withUsername(“sa”) .withPassword(“sa”); postgreSQLContainer .withInitScript(“some/location/on/classpath/someScript.sql”); postgreSQLContainer.start(); } JdbcDatabaseContainer is superclass of PostgreSQLContainer … Read more

Spring Boot TestContainers Mapped port can only be obtained after the container is started

You are trying to use PostgresSQLContainer as JUnit ClassRule but your usage of @ExtendWith seems to indicate that you are using JUnit 5 / Jupiter which does not support JUnit 4 rules. Use the JUnit 5 integration of Testcontainers instead: https://www.testcontainers.org/test_framework_integration/junit_5/

How to reuse Testcontainers between multiple SpringBootTests?

You can’t use the JUnit Jupiter annotation @Container if you want to have reusable containers. This annotation ensures to stop the container after each test. What you need is the singleton container approach, and use e.g. @BeforeAll to start your containers. Even though you then have .start() in multiple tests, Testcontainers won’t start a new … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)