Closed jomatt closed 6 months ago
Hey @jomatt, you need to store your data in the template1 database. So, remove the .withDatabaseName("postgres")
line from the container customizer and just change the target database in the init script to template1
. Let me know if it works.
@tomix26 thx for the swift answer - it works like a charm.
Another question: is there a way to programatically reset the DB without using the refresh
property on the @AutoConfigureEmbeddedDatabase
annotation? We have some tests that need to reset the state of the DB and others that don't. So it would be helpful to define for each test separately if the DB should be reset or not - instead of depending on a fixed refresh mode like AFTER_CLASS
or AFTER_EACH_TEST_METHOD
If you are using Flyway, you can use the @FlywayTest
annotation instead. It's possible to place the annotation even on a method level and the effect is the same as in case of the @AutoConfigureEmbeddedDatabase(refresh = ...)
annotation, check out an example here.
Nevertheless, if you really want to control the reset programmatically, you can inject an embedded data source into your test class and use a lower-level API of the library to get a corresponding database context and reset it manually. See the snippet below.
@RunWith(SpringRunner.class)
@AutoConfigureEmbeddedDatabase
public class DatabaseContextIntegrationTest {
@Autowired
private DataSource dataSource;
@Test
public void testMethod1() {
// Potentially dirty database from previous tests
}
@Test
public void testMethod2() {
DatabaseContext databaseContext = AopProxyUtils.getDatabaseContext(dataSource);
databaseContext.reset();
// Fresh database ensured by manual reset
}
@Test
public void testMethod3() {
// Dirty database that contains data from the previous test
}
}
Note that the order in which each test method is called can be non-deterministic, so the comments are just for illustrative purposes.
Ok, I guess it solved the problem. So, I'm closing the issue. Feel free to reopen it if you have more questions.
This library seems to be exactly what I was looking for. I have a Spring Boot project with Flyway. In order to speed up integration tests, I want to make use of the templating feature that is mentioned in the README:
So I built a custom docker image including test data inside the
postgres
database. I also created aPostgreSQLContainerCustomizer
bean to explicitly set the database name, assuming that the library would pick up this database name and use this DB as the template.However, I found out that the library always creates a clean database inside my custom docker image when I start my integration tests. This clean DB is then picked up by Flyway to run the migrations (instead of using the already populated and migrated
postgres
DB inside the custom docker image):This is my
Dockerfile
that I use to build the custom image:and the
init_db.sh
file:Am I missing something? Any help would be greatly appreciated.