Test XML, Web, & @TestPropertySource AOT support

See gh-28204, gh-28205
This commit is contained in:
Sam Brannen
2022-08-18 17:43:35 +02:00
parent 84c377965c
commit b0d65709ac
10 changed files with 541 additions and 31 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.test.context.aot;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
@@ -29,6 +28,7 @@ import org.springframework.aot.generate.GeneratedFiles.Kind;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.javapoet.ClassName;
@@ -38,8 +38,21 @@ import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTest
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.aot.samples.web.WebSpringJupiterTests;
import org.springframework.test.context.aot.samples.web.WebSpringTestNGTests;
import org.springframework.test.context.aot.samples.web.WebSpringVintageTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.function.ThrowingConsumer;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
/**
* Tests for {@link TestContextAotGenerator}.
@@ -76,50 +89,95 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
}
@Test
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
void generateApplicationContextInitializer() {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
void processAheadOfTimeWithBasicTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class,
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterSharedConfigTests.class);
List<ClassName> classNames = new ArrayList<>();
testClasses.forEach(testClass -> {
DefaultGenerationContext generationContext = generator.createGenerationContext(testClass);
MergedContextConfiguration mergedConfig = generator.buildMergedContextConfiguration(testClass);
ClassName className = generator.processAheadOfTime(mergedConfig, generationContext);
assertThat(className).isNotNull();
classNames.add(className);
generationContext.writeGeneratedContent();
});
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class);
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();
compile(generatedFiles, classNames, context -> {
MessageService messageService = context.getBean(MessageService.class);
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
// TODO Support @TestPropertySource in AOT testing mode.
// assertThat(context.getEnvironment().getProperty("test.engine"))
// .as("@TestPropertySource").isNotNull();
});
}
@Test
void processAheadOfTimeWithXmlTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
XmlSpringJupiterTests.class,
XmlSpringTestNGTests.class,
XmlSpringVintageTests.class);
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();
MessageService messageService = context.getBean(MessageService.class);
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
});
}
@Test
void processAheadOfTimeWithWebTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
WebSpringJupiterTests.class,
WebSpringTestNGTests.class,
WebSpringVintageTests.class);
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();
MockMvc mockMvc = webAppContextSetup((WebApplicationContext) context).build();
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
});
}
@SuppressWarnings("unchecked")
private void compile(InMemoryGeneratedFiles generatedFiles, List<ClassName> classNames,
Consumer<GenericApplicationContext> result) {
private void processAheadOfTime(Set<Class<?>> testClasses, ThrowingConsumer<ApplicationContext> result) {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
List<Mapping> mappings = processAheadOfTime(generator, testClasses);
TestCompiler.forSystem().withFiles(generatedFiles).compile(compiled -> {
classNames.forEach(className -> {
GenericApplicationContext gac = new GenericApplicationContext();
mappings.forEach(mapping -> {
MergedContextConfiguration mergedConfig = mapping.mergedConfig();
ApplicationContextInitializer<GenericApplicationContext> contextInitializer =
compiled.getInstance(ApplicationContextInitializer.class, className.reflectionName());
contextInitializer.initialize(gac);
gac.refresh();
result.accept(gac);
compiled.getInstance(ApplicationContextInitializer.class, mapping.className().reflectionName());
AotRuntimeContextLoader aotRuntimeContextLoader = new AotRuntimeContextLoader();
GenericApplicationContext context = aotRuntimeContextLoader.loadContext(mergedConfig, contextInitializer);
result.accept(context);
});
});
}
private List<Mapping> processAheadOfTime(TestContextAotGenerator generator, Set<Class<?>> testClasses) {
List<Mapping> mappings = new ArrayList<>();
testClasses.forEach(testClass -> {
DefaultGenerationContext generationContext = generator.createGenerationContext(testClass);
MergedContextConfiguration mergedConfig = generator.buildMergedContextConfiguration(testClass);
ClassName className = generator.processAheadOfTime(mergedConfig, generationContext);
assertThat(className).isNotNull();
mappings.add(new Mapping(mergedConfig, className));
generationContext.writeGeneratedContent();
});
return mappings;
}
record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Sam Brannen
* @since 6.0
*/
@RestController
class MessageController {
@GetMapping("/hello")
String hello() {
return "Hello, AOT!";
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
/**
* @author Sam Brannen
* @since 6.0
*/
@SpringJUnitWebConfig(WebTestConfiguration.class)
@TestPropertySource(properties = "test.engine = jupiter")
public class WebSpringJupiterTests {
MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
@org.junit.jupiter.api.BeforeEach
void setUpMockMvc() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@org.junit.jupiter.api.Test
void test(@Value("${test.engine}") String testEngine) throws Exception {
assertThat(testEngine)
.as("@Value").isEqualTo("jupiter");
assertThat(wac.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("jupiter");
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
/**
* @author Sam Brannen
* @since 6.0
*/
@ContextConfiguration(classes = WebTestConfiguration.class)
@WebAppConfiguration
@TestPropertySource(properties = "test.engine = testng")
public class WebSpringTestNGTests extends AbstractTestNGSpringContextTests {
MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
@Value("${test.engine}")
String testEngine;
@org.testng.annotations.BeforeMethod
public void setUpMockMvc() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@org.testng.annotations.Test
public void test() throws Exception {
assertThat(testEngine)
.as("@Value").isEqualTo("testng");
assertThat(wac.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("testng");
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.web;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
/**
* @author Sam Brannen
* @since 6.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = WebTestConfiguration.class)
@WebAppConfiguration
@TestPropertySource(properties = "test.engine = vintage")
public class WebSpringVintageTests {
MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
@Value("${test.engine}")
String testEngine;
@org.junit.Before
public void setUpMockMvc() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@org.junit.Test
public void test() throws Exception {
assertThat(testEngine)
.as("@Value").isEqualTo("vintage");
assertThat(wac.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("vintage");
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.web;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.EnableWebFlux;
/**
* @author Sam Brannen
* @since 6.0
*/
@Configuration(proxyBeanMethods = false)
@EnableWebFlux
class WebTestConfiguration {
@Bean
MessageController messageController() {
return new MessageController();
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.xml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sam Brannen
* @since 6.0
*/
@SpringJUnitConfig(locations = "test-config.xml")
@TestPropertySource(properties = "test.engine = jupiter")
public class XmlSpringJupiterTests {
@Autowired
ApplicationContext context;
@Autowired
MessageService messageService;
@Value("${test.engine}")
String testEngine;
@org.junit.jupiter.api.Test
void test() {
assertThat(testEngine)
.as("@Value").isEqualTo("jupiter");
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("jupiter");
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.xml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sam Brannen
* @since 6.0
*/
@ContextConfiguration("test-config.xml")
@TestPropertySource(properties = "test.engine = testng")
public class XmlSpringTestNGTests extends AbstractTestNGSpringContextTests {
@Autowired
ApplicationContext context;
@Autowired
MessageService messageService;
@Value("${test.engine}")
String testEngine;
@org.testng.annotations.Test
public void test() {
assertThat(testEngine)
.as("@Value").isEqualTo("testng");
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("testng");
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.aot.samples.xml;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sam Brannen
* @since 6.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("test-config.xml")
@TestPropertySource(properties = "test.engine = vintage")
public class XmlSpringVintageTests {
@Autowired
ApplicationContext context;
@Autowired
MessageService messageService;
@Value("${test.engine}")
String testEngine;
@org.junit.Test
public void test() {
assertThat(testEngine)
.as("@Value").isEqualTo("vintage");
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isEqualTo("vintage");
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
}
}