16
16
17
17
package org .springframework .aot .test .boot ;
18
18
19
+ import java .lang .reflect .Method ;
19
20
import java .util .ArrayList ;
20
21
import java .util .Collections ;
21
22
import java .util .List ;
23
+ import java .util .Set ;
22
24
23
25
import org .springframework .aot .SpringApplicationAotUtils ;
24
26
import org .springframework .boot .ApplicationContextFactory ;
33
35
import org .springframework .context .ApplicationContextInitializer ;
34
36
import org .springframework .context .ConfigurableApplicationContext ;
35
37
import org .springframework .core .env .ConfigurableEnvironment ;
38
+ import org .springframework .test .context .ContextCustomizer ;
36
39
import org .springframework .test .context .MergedContextConfiguration ;
37
40
import org .springframework .test .context .SmartContextLoader ;
38
41
import org .springframework .test .context .support .TestPropertySourceUtils ;
39
42
import org .springframework .test .context .web .WebMergedContextConfiguration ;
43
+ import org .springframework .util .Assert ;
44
+ import org .springframework .util .ClassUtils ;
40
45
import org .springframework .util .ObjectUtils ;
46
+ import org .springframework .util .ReflectionUtils ;
41
47
import org .springframework .web .context .support .GenericWebApplicationContext ;
42
48
43
49
/**
49
55
*/
50
56
public class AotSpringBootConfigContextLoader extends SpringBootContextLoader {
51
57
58
+ // "SpringBootTestArgs#get(Set)" static method
59
+ private static final Method SPRING_BOOT_TEST_ARGS__GET ;
60
+
61
+ static {
62
+ Class <?> clazz ;
63
+ try {
64
+ // SpringBootTestArgs is a package private class
65
+ clazz = ClassUtils .forName ("org.springframework.boot.test.context.SpringBootTestArgs" , null );
66
+ } catch (ClassNotFoundException ex ) {
67
+ throw new IllegalStateException ("Failed to load SpringBootTestArgs class" , ex );
68
+ }
69
+ Method method = ReflectionUtils .findMethod (clazz , "get" , Set .class );
70
+ Assert .notNull (method , "SpringBootTestArgs#get(Set) method must exist" );
71
+ ReflectionUtils .makeAccessible (method );
72
+ SPRING_BOOT_TEST_ARGS__GET = method ;
73
+ }
74
+
52
75
private final Class <? extends ApplicationContextInitializer <?>> testContextInitializer ;
53
76
54
77
private final WebApplicationType webApplicationType ;
@@ -80,9 +103,6 @@ public AotSpringBootConfigContextLoader(Class<? extends ApplicationContextInitia
80
103
81
104
@ Override
82
105
public ConfigurableApplicationContext loadContext (MergedContextConfiguration config ) {
83
- // TODO: handle application arguments
84
- String [] args = new String [0 ];
85
-
86
106
SpringApplication application = new AotTestSpringApplication (config .getTestClass ().getClassLoader (), testContextInitializer );
87
107
application .setMainApplicationClass (config .getTestClass ());
88
108
application .setSources (Collections .singleton (testContextInitializer .getName ()));
@@ -108,6 +128,8 @@ else if (this.webApplicationType == WebApplicationType.REACTIVE) {
108
128
ApplicationContextFactory .of (GenericReactiveWebApplicationContext ::new ));
109
129
}
110
130
}
131
+
132
+ String [] args = resolveArguments (config .getContextCustomizers ());
111
133
ConfigurableApplicationContext context = application .run (args );
112
134
113
135
return context ;
@@ -128,6 +150,10 @@ private void setActiveProfiles(ConfigurableEnvironment environment, String[] pro
128
150
TestPropertyValues .of (pairs ).applyTo (environment );
129
151
}
130
152
153
+ private String [] resolveArguments (Set <ContextCustomizer > customizers ) {
154
+ return (String []) ReflectionUtils .invokeMethod (SPRING_BOOT_TEST_ARGS__GET , null , customizers );
155
+ }
156
+
131
157
private static class WebConfigurer {
132
158
133
159
void configure (MergedContextConfiguration configuration , SpringApplication application ,
0 commit comments