Configure JUnit platform with Spring Boot 2.2+

Closes gh-904
This commit is contained in:
Stephane Nicoll
2019-05-21 13:31:00 +02:00
parent 75228ba4a3
commit 9ea41f151d
3 changed files with 65 additions and 0 deletions

View File

@@ -223,6 +223,13 @@ public class GradleProjectGenerationConfiguration {
projectDescription.getPlatformVersion().toString());
}
@Bean
@ConditionalOnPlatformVersion("2.2.0.M3")
public BuildCustomizer<GradleBuild> testTaskContributor() {
return (build) -> build.customizeTask("test",
(test) -> test.invoke("useJUnitPlatform"));
}
@Bean
public GradleAnnotationProcessorScopeBuildCustomizer gradleAnnotationProcessorScopeBuildCustomizer() {
return new GradleAnnotationProcessorScopeBuildCustomizer();
@@ -264,6 +271,13 @@ public class GradleProjectGenerationConfiguration {
};
}
@Bean
@ConditionalOnPlatformVersion("2.2.0.M3")
public BuildCustomizer<GradleBuild> testTaskContributor() {
return (build) -> build.customizeTasksWithType("Test",
(test) -> test.invoke("useJUnitPlatform"));
}
}
}

View File

@@ -55,6 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link GradleProjectGenerationConfiguration} with Kotlin DSL build system.
*
* @author Jean-Baptiste Nizet
* @author Stephane Nicoll
*/
class GradleKtsProjectGenerationConfigurationTests {
@@ -155,6 +156,32 @@ class GradleKtsProjectGenerationConfigurationTests {
}
}
@Test
void junitPlatformIsConfiguredWithCompatibleVersion() {
ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.2.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure projectStructure = this.projectTester.generate(description);
assertThat(projectStructure.getRelativePathsOfProjectFiles())
.contains("build.gradle.kts");
List<String> lines = projectStructure.readAllLines("build.gradle.kts");
assertThat(lines).containsSequence("tasks.withType<Test> {",
" useJUnitPlatform()", "}");
}
@Test
void junitPlatformIsNotConfiguredWithIncompatibleVersion() {
ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.1.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure projectStructure = this.projectTester.generate(description);
assertThat(projectStructure.getRelativePathsOfProjectFiles())
.contains("build.gradle.kts");
List<String> lines = projectStructure.readAllLines("build.gradle.kts");
assertThat(lines).doesNotContainSequence("tasks.withType<Test> {",
" useJUnitPlatform()", "}");
}
private static String[] readAllLines(Path file) throws IOException {
String content = StreamUtils.copyToString(
new FileInputStream(new File(file.toString())), StandardCharsets.UTF_8);

View File

@@ -154,6 +154,30 @@ class GradleProjectGenerationConfigurationTests {
}
}
@Test
void junitPlatformIsConfiguredWithCompatibleVersion() {
ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.2.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure projectStructure = this.projectTester.generate(description);
assertThat(projectStructure.getRelativePathsOfProjectFiles())
.contains("build.gradle");
List<String> lines = projectStructure.readAllLines("build.gradle");
assertThat(lines).containsSequence("test {", " useJUnitPlatform()", "}");
}
@Test
void junitPlatformIsNotConfiguredWithIncompatibleVersion() {
ProjectDescription description = new ProjectDescription();
description.setPlatformVersion(Version.parse("2.1.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure projectStructure = this.projectTester.generate(description);
assertThat(projectStructure.getRelativePathsOfProjectFiles())
.contains("build.gradle");
List<String> lines = projectStructure.readAllLines("build.gradle");
assertThat(lines).doesNotContainSequence("test {", " useJUnitPlatform()", "}");
}
static Stream<Arguments> annotationProcessorScopeBuildParameters() {
return Stream.of(Arguments.arguments("1.5.17.RELEASE", false),
Arguments.arguments("2.0.6.RELEASE", true),