|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Set;
|
22 |
| -import java.util.function.Consumer; |
23 | 22 | import java.util.stream.Stream;
|
24 | 23 |
|
25 | 24 | import org.junit.jupiter.api.Test;
|
|
29 | 28 | import org.springframework.aot.generate.InMemoryGeneratedFiles;
|
30 | 29 | import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess;
|
31 | 30 | import org.springframework.aot.test.generator.compile.TestCompiler;
|
| 31 | +import org.springframework.context.ApplicationContext; |
32 | 32 | import org.springframework.context.ApplicationContextInitializer;
|
33 | 33 | import org.springframework.context.support.GenericApplicationContext;
|
34 | 34 | import org.springframework.javapoet.ClassName;
|
|
38 | 38 | import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
|
39 | 39 | import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
|
40 | 40 | import org.springframework.test.context.aot.samples.common.MessageService;
|
| 41 | +import org.springframework.test.context.aot.samples.web.WebSpringJupiterTests; |
| 42 | +import org.springframework.test.context.aot.samples.web.WebSpringTestNGTests; |
| 43 | +import org.springframework.test.context.aot.samples.web.WebSpringVintageTests; |
| 44 | +import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests; |
| 45 | +import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests; |
| 46 | +import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests; |
| 47 | +import org.springframework.test.web.servlet.MockMvc; |
| 48 | +import org.springframework.util.function.ThrowingConsumer; |
| 49 | +import org.springframework.web.context.WebApplicationContext; |
41 | 50 |
|
42 | 51 | import static org.assertj.core.api.Assertions.assertThat;
|
| 52 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 53 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 54 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 55 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
43 | 56 |
|
44 | 57 | /**
|
45 | 58 | * Tests for {@link TestContextAotGenerator}.
|
@@ -76,50 +89,95 @@ void generate() {
|
76 | 89 | }
|
77 | 90 |
|
78 | 91 | @Test
|
79 |
| - // We cannot parameterize with the test classes, since @CompileWithTargetClassAccess |
80 |
| - // cannot support @ParameterizedTest methods. |
81 |
| - void generateApplicationContextInitializer() { |
82 |
| - InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
83 |
| - TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
| 92 | + void processAheadOfTimeWithBasicTests() { |
| 93 | + // We cannot parameterize with the test classes, since @CompileWithTargetClassAccess |
| 94 | + // cannot support @ParameterizedTest methods. |
84 | 95 | Set<Class<?>> testClasses = Set.of(
|
85 |
| - BasicSpringTestNGTests.class, |
86 |
| - BasicSpringVintageTests.class, |
| 96 | + BasicSpringJupiterSharedConfigTests.class, |
87 | 97 | BasicSpringJupiterTests.class,
|
88 |
| - BasicSpringJupiterSharedConfigTests.class); |
89 |
| - List<ClassName> classNames = new ArrayList<>(); |
90 |
| - testClasses.forEach(testClass -> { |
91 |
| - DefaultGenerationContext generationContext = generator.createGenerationContext(testClass); |
92 |
| - MergedContextConfiguration mergedConfig = generator.buildMergedContextConfiguration(testClass); |
93 |
| - ClassName className = generator.processAheadOfTime(mergedConfig, generationContext); |
94 |
| - assertThat(className).isNotNull(); |
95 |
| - classNames.add(className); |
96 |
| - generationContext.writeGeneratedContent(); |
| 98 | + BasicSpringJupiterTests.NestedTests.class, |
| 99 | + BasicSpringTestNGTests.class, |
| 100 | + BasicSpringVintageTests.class); |
| 101 | + |
| 102 | + processAheadOfTime(testClasses, context -> { |
| 103 | + assertThat(context.getEnvironment().getProperty("test.engine")) |
| 104 | + .as("Environment").isNotNull(); |
| 105 | + |
| 106 | + MessageService messageService = context.getBean(MessageService.class); |
| 107 | + assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); |
97 | 108 | });
|
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void processAheadOfTimeWithXmlTests() { |
| 113 | + // We cannot parameterize with the test classes, since @CompileWithTargetClassAccess |
| 114 | + // cannot support @ParameterizedTest methods. |
| 115 | + Set<Class<?>> testClasses = Set.of( |
| 116 | + XmlSpringJupiterTests.class, |
| 117 | + XmlSpringTestNGTests.class, |
| 118 | + XmlSpringVintageTests.class); |
| 119 | + |
| 120 | + processAheadOfTime(testClasses, context -> { |
| 121 | + assertThat(context.getEnvironment().getProperty("test.engine")) |
| 122 | + .as("Environment").isNotNull(); |
98 | 123 |
|
99 |
| - compile(generatedFiles, classNames, context -> { |
100 | 124 | MessageService messageService = context.getBean(MessageService.class);
|
101 | 125 | assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
|
102 |
| - // TODO Support @TestPropertySource in AOT testing mode. |
103 |
| - // assertThat(context.getEnvironment().getProperty("test.engine")) |
104 |
| - // .as("@TestPropertySource").isNotNull(); |
105 | 126 | });
|
106 | 127 | }
|
107 | 128 |
|
| 129 | + @Test |
| 130 | + void processAheadOfTimeWithWebTests() { |
| 131 | + // We cannot parameterize with the test classes, since @CompileWithTargetClassAccess |
| 132 | + // cannot support @ParameterizedTest methods. |
| 133 | + Set<Class<?>> testClasses = Set.of( |
| 134 | + WebSpringJupiterTests.class, |
| 135 | + WebSpringTestNGTests.class, |
| 136 | + WebSpringVintageTests.class); |
108 | 137 |
|
109 |
| - @SuppressWarnings("unchecked") |
110 |
| - private void compile(InMemoryGeneratedFiles generatedFiles, List<ClassName> classNames, |
111 |
| - Consumer<GenericApplicationContext> result) { |
| 138 | + processAheadOfTime(testClasses, context -> { |
| 139 | + assertThat(context.getEnvironment().getProperty("test.engine")) |
| 140 | + .as("Environment").isNotNull(); |
| 141 | + |
| 142 | + MockMvc mockMvc = webAppContextSetup((WebApplicationContext) context).build(); |
| 143 | + mockMvc.perform(get("/hello")) |
| 144 | + .andExpectAll(status().isOk(), content().string("Hello, AOT!")); |
| 145 | + }); |
| 146 | + } |
112 | 147 |
|
| 148 | + |
| 149 | + @SuppressWarnings("unchecked") |
| 150 | + private void processAheadOfTime(Set<Class<?>> testClasses, ThrowingConsumer<ApplicationContext> result) { |
| 151 | + InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles(); |
| 152 | + TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles); |
| 153 | + List<Mapping> mappings = processAheadOfTime(generator, testClasses); |
113 | 154 | TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> {
|
114 |
| - classNames.forEach(className -> { |
115 |
| - GenericApplicationContext gac = new GenericApplicationContext(); |
| 155 | + mappings.forEach(mapping -> { |
| 156 | + MergedContextConfiguration mergedConfig = mapping.mergedConfig(); |
116 | 157 | ApplicationContextInitializer<GenericApplicationContext> contextInitializer =
|
117 |
| - compiled.getInstance(ApplicationContextInitializer.class, className.reflectionName()); |
118 |
| - contextInitializer.initialize(gac); |
119 |
| - gac.refresh(); |
120 |
| - result.accept(gac); |
| 158 | + compiled.getInstance(ApplicationContextInitializer.class, mapping.className().reflectionName()); |
| 159 | + AotRuntimeContextLoader aotRuntimeContextLoader = new AotRuntimeContextLoader(); |
| 160 | + GenericApplicationContext context = aotRuntimeContextLoader.loadContext(mergedConfig, contextInitializer); |
| 161 | + result.accept(context); |
121 | 162 | });
|
122 | 163 | });
|
123 | 164 | }
|
124 | 165 |
|
| 166 | + private List<Mapping> processAheadOfTime(TestContextAotGenerator generator, Set<Class<?>> testClasses) { |
| 167 | + List<Mapping> mappings = new ArrayList<>(); |
| 168 | + testClasses.forEach(testClass -> { |
| 169 | + DefaultGenerationContext generationContext = generator.createGenerationContext(testClass); |
| 170 | + MergedContextConfiguration mergedConfig = generator.buildMergedContextConfiguration(testClass); |
| 171 | + ClassName className = generator.processAheadOfTime(mergedConfig, generationContext); |
| 172 | + assertThat(className).isNotNull(); |
| 173 | + mappings.add(new Mapping(mergedConfig, className)); |
| 174 | + generationContext.writeGeneratedContent(); |
| 175 | + }); |
| 176 | + return mappings; |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + record Mapping(MergedContextConfiguration mergedConfig, ClassName className) { |
| 181 | + } |
| 182 | + |
125 | 183 | }
|
0 commit comments