I'm trying to use TestContainer for an integration test of a Spring Boot application.
The database of such application resides in a custom PostgreSQL Docker image.
In the integration test the ApplicationContext is started through MockMvc and the container is started with something like
public class ITMyServiceTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Rule
public PostgreSQLContainer testPostgres = new PostgreSQLContainer("my-custom-database-image")
.withDatabaseName("my_db")
.withUsername("my_name")
.withPassword("my_pwd");
@Test
public void shouldDoSomething() throws Exception {
this.mockMvc.perform(get("/api/do/something")).andDo(print());
}
What happens now is that the container is started, but the application context doesn't refer to it.
I can't use the JDBC URL scheme (like spring.datasource.url=jdbc:tc:postgresql://localhost/my_db
) in a .properties
file because:
if I specify postgresql as server it will start another server and the context will use it
if I specify the name of my container (or everything else) the test will rightly raise a
java.lang.IllegalArgumentException
because it is not a known database type.
How can I set the Spring' application context to refer to my container?