CONTEXT:
I have a non Spring Boot
app which I wrap in Spring Boot in order to test functionality. It happened that code had an in-memory database in tests, I want to test against a different database to reproduce the production environment. I decided to go with testcontainers
to keep all existing tests untouched.
PROBLEM:
I need to load millions of rows into the test container. The question is how to populate the database when in testcontainers
? I found a similar question -> How to populate testcontainers
? but I still cannot get how to populate data in it.
How can I populate data in testcontainers
?
DatabaseTestInitalizer.java
I use for instantiating container:
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.testcontainers.containers.MSSQLServerContainer;
public class DatabaseTestInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private MSSQLServerContainer mssqlServerContainer;
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
mssqlServerContainer = new MSSQLServerContainer();
mssqlServerContainer.start();
// This is solution for 1.x.x Spring Boot framework
// Article for migration from 1.x.x to 2.x.x Spring Boot https://stackoverflow.com/questions/54718995/appropriate-usage-of-testpropertyvalues-in-spring-boot-tests
EnvironmentTestUtils.addEnvironment(configurableApplicationContext.getEnvironment(),
"spring.datasource.url=" + mssqlServerContainer.getJdbcUrl(),
"spring.datasource.username=" + mssqlServerContainer.getUsername(),
"spring.datasource.password=" + mssqlServerContainer.getPassword()
);
}
}