11package de .kuksin .testcontainers ;
22
3+ import org .jetbrains .annotations .NotNull ;
34import org .springframework .context .ApplicationContextInitializer ;
45import org .springframework .context .ConfigurableApplicationContext ;
56import org .springframework .core .env .ConfigurableEnvironment ;
@@ -19,10 +20,13 @@ static class Initializer
1920
2021 static PostgreSQLContainer <?> postgres = new PostgreSQLContainer <>();
2122
22- public static Map < String , String > getProperties () {
23+ private static void startContainers () {
2324 Startables .deepStart (Stream .of (postgres )).join ();
2425 // we can add further containers here like rabbitmq or other database
26+ }
2527
28+ @ NotNull
29+ private static Map <String , String > createConnectionConfiguration () {
2630 return Map .of (
2731 "spring.datasource.url" , postgres .getJdbcUrl (),
2832 "spring.datasource.username" , postgres .getUsername (),
@@ -33,10 +37,11 @@ public static Map<String, String> getProperties() {
3337
3438 @ Override
3539 public void initialize (ConfigurableApplicationContext applicationContext ) {
40+ startContainers ();
3641 ConfigurableEnvironment environment = applicationContext .getEnvironment ();
3742 MapPropertySource testcontainers = new MapPropertySource (
3843 "testcontainers" ,
39- (Map ) getProperties ()
44+ (Map ) createConnectionConfiguration ()
4045 );
4146 environment .getPropertySources ().addFirst (testcontainers );
4247 }
0 commit comments