Yes, it can be done using profiles.
One possible solution would be (the idea is to play around with static
keyword and it assumes using .withLocalCompose(true)
):
@Configuration
@Profile("test")
public class TestDockerConfig {
// initialize your containers in static fields/static block
}
And use the test profile when you need it. Even if you import that configuration in all tests, it should only be loaded for "test" ones.
The idea is to have docker environment provided to test suite and use property profiles.
It can either:
- be provided by local docker engine ("dev") where you start containers
yourself with proper dev URLs specified in application-dev.properties
- or provided via TestContainers, with test URLs in
application-test.properties
Since starting up containers takes time you want to do that only once in a static way and it will be loaded before all of your classes.
Hope that helps.