Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit ca1e5ba

Browse files
committed
Polish "Support "args" param in "@SpringBootTest""
See gh-1447
1 parent 8ba22ab commit ca1e5ba

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

spring-aot-test/src/main/java/org/springframework/aot/test/boot/SpringBootAotTestContextProcessor.java

-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public GenericApplicationContext prepareTestContext(MergedContextConfiguration c
6767
@Override
6868
public CodeBlock writeInstanceSupplier(MergedContextConfiguration config, ClassName applicationContextInitializer) {
6969
String[] args = SpringBootTestArgsAccessor.get(config.getContextCustomizers());
70-
7170
Builder code = CodeBlock.builder();
7271
code.add("() -> new $T($T.class", AotSpringBootConfigContextLoader.class, applicationContextInitializer);
7372
WebApplicationType webApplicationType = detectWebApplicationType(config);
@@ -81,7 +80,6 @@ public CodeBlock writeInstanceSupplier(MergedContextConfiguration config, ClassN
8180
code.add(", $L", multi.join(", "));
8281
}
8382
code.add(")");
84-
8583
return code.build();
8684
}
8785

spring-aot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgsAccessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.springframework.test.context.ContextCustomizer;
2222

2323
/**
24-
* Provide access to the package private {@link SpringBootTestArgs} class.
24+
* Accessor to privileged methods of {@link SpringBootTestArgs}.
2525
*
2626
* @author Tadaya Tsuyukubo
2727
*/

spring-native/src/test/java/org/springframework/aot/test/boot/AotSpringBootConfigContextLoaderTests.java

+25-33
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
package org.springframework.aot.test.boot;
1818

1919
import java.util.Collections;
20-
import java.util.List;
2120
import java.util.Map;
22-
import java.util.Map.Entry;
23-
import java.util.Set;
2421
import java.util.function.Consumer;
2522
import java.util.function.Supplier;
26-
import java.util.stream.Collectors;
2723
import java.util.stream.Stream;
2824

2925
import org.junit.jupiter.api.Test;
@@ -95,36 +91,40 @@ void loadContextSetActiveProfiles() {
9591
assertThat(context.getEnvironment().getActiveProfiles()).containsOnly("profile1", "profile2"));
9692
}
9793

94+
static Stream<Arguments> applicationArguments() {
95+
return Stream.of(
96+
Arguments.of(new String[] {}, Collections.emptyMap()),
97+
Arguments.of(new String[] { "--app.test=one" }, Map.of("app.test", "one")),
98+
Arguments.of(new String[] { "--app.test=one", "--app.name=foo" }, Map.of("app.test", "one", "app.name", "foo"))
99+
);
100+
}
101+
98102
@ParameterizedTest
99103
@MethodSource("applicationArguments")
100-
void loadContextUsesArguments(String[] arguments, List<Entry<String, String>> expectedEntries) {
104+
void loadContextUsesArguments(String[] arguments, Map<String, String> expectedEntries) {
101105
AotSpringBootConfigContextLoader loader = new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class, arguments);
102-
run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)), (context) -> {
103-
ApplicationArguments args = context.getBean(ApplicationArguments.class);
104-
Set<String> keys = expectedEntries.stream().map(Entry::getKey).collect(Collectors.toSet());
105-
assertThat(args.getOptionNames()).containsExactlyElementsOf(keys);
106-
for (Entry<String, String> expectedEntry : expectedEntries) {
107-
String key = expectedEntry.getKey();
108-
String value = expectedEntry.getValue();
109-
assertThat(args.getOptionValues(key)).containsOnly(value);
110-
}
111-
});
106+
run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)),
107+
validateApplicationArguments(expectedEntries));
112108
}
113109

114110
@ParameterizedTest
115111
@MethodSource("applicationArguments")
116-
void loadContextUsesArgumentsAndWebSettings(String[] arguments, List<Entry<String, String>> expectedEntries) {
117-
AotSpringBootConfigContextLoader loader = new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class, WebApplicationType.SERVLET, WebEnvironment.MOCK, arguments);
118-
run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)), (context) -> {
112+
void loadContextUsesArgumentsAndWebSettings(String[] arguments, Map<String, String> expectedEntries) {
113+
AotSpringBootConfigContextLoader loader = new AotSpringBootConfigContextLoader(
114+
TestApplicationContextInitializer.class, WebApplicationType.SERVLET, WebEnvironment.MOCK, arguments);
115+
run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)),
116+
validateApplicationArguments(expectedEntries));
117+
}
118+
119+
private Consumer<AssertableApplicationContext> validateApplicationArguments(
120+
Map<String, String> expectedEntries) {
121+
return (context) -> {
119122
ApplicationArguments args = context.getBean(ApplicationArguments.class);
120-
Set<String> keys = expectedEntries.stream().map(Entry::getKey).collect(Collectors.toSet());
121-
assertThat(args.getOptionNames()).containsExactlyElementsOf(keys);
122-
for (Entry<String, String> expectedEntry : expectedEntries) {
123-
String key = expectedEntry.getKey();
124-
String value = expectedEntry.getValue();
125-
assertThat(args.getOptionValues(key)).containsOnly(value);
123+
assertThat(args.getOptionNames()).containsExactlyInAnyOrderElementsOf(expectedEntries.keySet());
124+
for (String optionName : args.getOptionNames()) {
125+
assertThat(args.getOptionValues(optionName)).containsOnly(expectedEntries.get(optionName));
126126
}
127-
});
127+
};
128128
}
129129

130130
private void run(Supplier<ConfigurableApplicationContext> supplier, Consumer<AssertableApplicationContext> context) {
@@ -168,12 +168,4 @@ public void initialize(GenericApplicationContext applicationContext) {
168168

169169
}
170170

171-
static Stream<Arguments> applicationArguments() {
172-
return Stream.of(
173-
Arguments.of(new String[] {}, Collections.emptyList()),
174-
Arguments.of(new String[] { "--app.test=one"}, List.of(Map.entry("app.test", "one"))),
175-
Arguments.of(new String[] { "--app.test=one", "--app.name=foo" }, List.of(Map.entry("app.test", "one"), Map.entry("app.name", "foo")))
176-
);
177-
}
178-
179171
}

0 commit comments

Comments
 (0)