I’m working on a Spring Boot application that uses two databases: local and global. I use Flyway for migrations for both databases. The global database is managed and modified by an external small app(may not be the best solution but it works). The point is that the big application that I want to test uses both databases.
I’m encountering problems when trying to test my application. I have tried two approaches:
Separate Containers Approach: I tried running two MariaDB containers, applying the global migrations from the test resources to the appropriate container. Here is a small code snippet, for easier understanding:
@Container
public static final MariaDBContainer<?> localDbContainer = new MariaDBContainer<>("mariadb:latest")
.withDatabaseName("localdb")
.withUsername("test")
.withPassword("test");
@Container
public static final MariaDBContainer<?> globalDbContainer = new MariaDBContainer<>("mariadb:latest")
.withDatabaseName("globaldb")
.withUsername("test")
.withPassword("test");
@BeforeAll
public static void setUp() {
Flyway localFlyway = Flyway.configure()
.dataSource(localDbContainer.getJdbcUrl(), localDbContainer.getUsername(), localDbContainer.getPassword())
.locations("classpath:db/migration/mariadb")
.load();
localFlyway.migrate();
Flyway globalFlyway = Flyway.configure()
.dataSource(globalDbContainer.getJdbcUrl(), globalDbContainer.getUsername(), globalDbContainer.getPassword())
.locations("classpath:db/migration/test")
.load();
globalFlyway.migrate();
}
Single Container Approach: I also attempted to use a single MariaDB container and applied the global migrations during the test runtime. This approach also didn’t work as expected.
However, the tests either fail to locate the databases or create test entities in the application’s own database instead of the test databases.
The reason why I think it is not correct to try to run global migrations as an add-on to have global and local tables in the same place during test is because the application is configured to use two separate databases with two transaction managers etc.
How can I configure my tests to use both test databases properly?