1
1
package de .kuksin .testcontainers ;
2
2
3
+ import org .jetbrains .annotations .NotNull ;
3
4
import org .springframework .context .ApplicationContextInitializer ;
4
5
import org .springframework .context .ConfigurableApplicationContext ;
5
6
import org .springframework .core .env .ConfigurableEnvironment ;
@@ -19,10 +20,13 @@ static class Initializer
19
20
20
21
static PostgreSQLContainer <?> postgres = new PostgreSQLContainer <>();
21
22
22
- public static Map < String , String > getProperties () {
23
+ private static void startContainers () {
23
24
Startables .deepStart (Stream .of (postgres )).join ();
24
25
// we can add further containers here like rabbitmq or other database
26
+ }
25
27
28
+ @ NotNull
29
+ private static Map <String , String > createConnectionConfiguration () {
26
30
return Map .of (
27
31
"spring.datasource.url" , postgres .getJdbcUrl (),
28
32
"spring.datasource.username" , postgres .getUsername (),
@@ -33,10 +37,11 @@ public static Map<String, String> getProperties() {
33
37
34
38
@ Override
35
39
public void initialize (ConfigurableApplicationContext applicationContext ) {
40
+ startContainers ();
36
41
ConfigurableEnvironment environment = applicationContext .getEnvironment ();
37
42
MapPropertySource testcontainers = new MapPropertySource (
38
43
"testcontainers" ,
39
- (Map ) getProperties ()
44
+ (Map ) createConnectionConfiguration ()
40
45
);
41
46
environment .getPropertySources ().addFirst (testcontainers );
42
47
}
0 commit comments