Skip to content

Commit

Permalink
Remove JUnit 4 support
Browse files Browse the repository at this point in the history
This commit also upgrades the test suite to use a more recent version as
removing support for JUnit4 effectively removes support for those as
well.

Closes gh-1219
  • Loading branch information
snicoll committed Apr 13, 2021
1 parent 6377ac7 commit b3c0f16
Show file tree
Hide file tree
Showing 39 changed files with 160 additions and 639 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -40,7 +40,7 @@ class ActuatorIntegrationTests extends AbstractFullStackInitializrIntegrationTes
void infoHasExternalProperties() {
String body = getRestTemplate().getForObject(createUrl("/actuator/info"), String.class);
assertThat(body).contains("\"spring-boot\"");
assertThat(body).contains("\"version\":\"2.1.4.RELEASE\"");
assertThat(body).contains("\"version\":\"2.4.4\"");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -39,24 +39,6 @@
@ProjectGenerationConfiguration
public class BuildProjectGenerationConfiguration {

@Bean
@ConditionalOnPlatformVersion("[2.0.0.RELEASE,2.2.0.M3)")
public BuildCustomizer<Build> junit4TestStarterContributor() {
return (build) -> build.dependencies().add("test", "org.springframework.boot", "spring-boot-starter-test",
DependencyScope.TEST_COMPILE);
}

@Bean
@ConditionalOnPlatformVersion("[2.2.0.M3,2.2.0.M4]")
@Deprecated
public BuildCustomizer<Build> junit5LegacyTestStarterContributor() {
return (build) -> build.dependencies().add("test",
Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter-test")
.scope(DependencyScope.TEST_COMPILE)
.exclusions(new Exclusion("org.junit.vintage", "junit-vintage-engine"),
new Exclusion("junit", "junit")));
}

@Bean
@ConditionalOnPlatformVersion("[2.2.0.M5,2.4.0-SNAPSHOT)")
public BuildCustomizer<Build> junit5TestStarterContributor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -43,17 +43,6 @@ public MainApplicationTypeCustomizer<TypeDeclaration> springBootApplicationAnnot
}

@Bean
@ConditionalOnPlatformVersion("[2.0.0.RELEASE,2.2.0.M3)")
public TestApplicationTypeCustomizer<TypeDeclaration> junit4SpringBootTestTypeCustomizer() {
return (typeDeclaration) -> {
typeDeclaration.annotate(Annotation.name("org.junit.runner.RunWith", (annotation) -> annotation
.attribute("value", Class.class, "org.springframework.test.context.junit4.SpringRunner")));
typeDeclaration.annotate(Annotation.name("org.springframework.boot.test.context.SpringBootTest"));
};
}

@Bean
@ConditionalOnPlatformVersion("2.2.0.M3")
public TestApplicationTypeCustomizer<TypeDeclaration> junitJupiterSpringBootTestTypeCustomizer() {
return (typeDeclaration) -> typeDeclaration
.annotate(Annotation.name("org.springframework.boot.test.context.SpringBootTest"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -23,7 +23,6 @@
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnPackaging;
import io.spring.initializr.generator.condition.ConditionalOnPlatformVersion;
import io.spring.initializr.generator.language.Annotation;
import io.spring.initializr.generator.language.Parameter;
import io.spring.initializr.generator.language.groovy.GroovyExpressionStatement;
Expand Down Expand Up @@ -61,18 +60,6 @@ MainApplicationTypeCustomizer<GroovyTypeDeclaration> mainMethodContributor() {
}

@Bean
@ConditionalOnPlatformVersion("[2.0.0.RELEASE,2.2.0.M3)")
TestApplicationTypeCustomizer<GroovyTypeDeclaration> junit4TestMethodContributor() {
return (typeDeclaration) -> {
GroovyMethodDeclaration method = GroovyMethodDeclaration.method("contextLoads").modifiers(Modifier.PUBLIC)
.returning("void").body();
method.annotate(Annotation.name("org.junit.Test"));
typeDeclaration.addMethodDeclaration(method);
};
}

@Bean
@ConditionalOnPlatformVersion("2.2.0.M3")
TestApplicationTypeCustomizer<GroovyTypeDeclaration> junitJupiterTestMethodContributor() {
return (typeDeclaration) -> {
GroovyMethodDeclaration method = GroovyMethodDeclaration.method("contextLoads").returning("void").body();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -19,7 +19,6 @@
import java.lang.reflect.Modifier;

import io.spring.initializr.generator.condition.ConditionalOnPackaging;
import io.spring.initializr.generator.condition.ConditionalOnPlatformVersion;
import io.spring.initializr.generator.language.Annotation;
import io.spring.initializr.generator.language.Parameter;
import io.spring.initializr.generator.language.java.JavaExpressionStatement;
Expand Down Expand Up @@ -59,19 +58,6 @@ MainApplicationTypeCustomizer<JavaTypeDeclaration> mainMethodContributor() {
}

@Bean
@ConditionalOnPlatformVersion("[2.0.0.RELEASE,2.2.0.M3)")
TestApplicationTypeCustomizer<JavaTypeDeclaration> junit4TestMethodContributor() {
return (typeDeclaration) -> {
typeDeclaration.modifiers(Modifier.PUBLIC);
JavaMethodDeclaration method = JavaMethodDeclaration.method("contextLoads").modifiers(Modifier.PUBLIC)
.returning("void").body();
method.annotate(Annotation.name("org.junit.Test"));
typeDeclaration.addMethodDeclaration(method);
};
}

@Bean
@ConditionalOnPlatformVersion("2.2.0.M3")
TestApplicationTypeCustomizer<JavaTypeDeclaration> junitJupiterTestMethodContributor() {
return (typeDeclaration) -> {
JavaMethodDeclaration method = JavaMethodDeclaration.method("contextLoads").returning("void").body();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -54,17 +54,6 @@
class KotlinProjectGenerationDefaultContributorsConfiguration {

@Bean
@ConditionalOnPlatformVersion("[2.0.0.RELEASE,2.2.0.M3)")
TestApplicationTypeCustomizer<KotlinTypeDeclaration> junit4TestMethodContributor() {
return (typeDeclaration) -> {
KotlinFunctionDeclaration function = KotlinFunctionDeclaration.function("contextLoads").body();
function.annotate(Annotation.name("org.junit.Test"));
typeDeclaration.addFunctionDeclaration(function);
};
}

@Bean
@ConditionalOnPlatformVersion("2.2.0.M3")
TestApplicationTypeCustomizer<KotlinTypeDeclaration> junitJupiterTestMethodContributor() {
return (typeDeclaration) -> {
KotlinFunctionDeclaration function = KotlinFunctionDeclaration.function("contextLoads").body();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -114,30 +114,6 @@ private void testCurrentGenerationWar(Language language, BuildSystem build, Stri
new ClassPathResource("project/" + language + "/standard/war-" + getAssertFileName(fileName)));
}

@ParameterizedTest
@MethodSource("parameters")
void previousGenerationJarJava(BuildSystem build, String fileName) {
testPreviousGenerationJar(java, build, fileName);
}

@ParameterizedTest
@MethodSource("parameters")
void previousGenerationJarGroovy(BuildSystem build, String fileName) {
testPreviousGenerationJar(groovy, build, fileName);
}

@ParameterizedTest
@MethodSource("parameters")
void previousGenerationJarKotlin(BuildSystem build, String fileName) {
testPreviousGenerationJar(kotlin, build, fileName);
}

private void testPreviousGenerationJar(Language language, BuildSystem build, String fileName) {
ProjectStructure project = generateProject(language, build, "2.1.0.RELEASE");
assertThat(project).textFile(fileName).hasSameContentAs(
new ClassPathResource("project/" + language + "/previous/" + getAssertFileName(fileName)));
}

@ParameterizedTest
@MethodSource("parameters")
void kotlinJava11(BuildSystem build, String fileName) {
Expand Down Expand Up @@ -259,7 +235,7 @@ void repositoriesMilestone(BuildSystem build, String fileName) {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().addDependencyGroup("test", foo)
.build();
ProjectStructure project = generateProject(java, build, "2.4.1", (description) -> {
description.setPlatformVersion(Version.parse("2.2.0.M1"));
description.setPlatformVersion(Version.parse("2.4.0-M1"));
description.addDependency("foo", MetadataBuildItemMapper.toDependency(foo));
}, metadata);
assertThat(project).textFile(fileName).hasSameContentAs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,36 @@ void gradleWrapperIsContributedWhenGeneratingGradleKtsProject(String platformVer
@Test
void buildDotGradleDotKtsIsContributedWhenGeneratingGradleKtsProject() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
description.setPlatformVersion(Version.parse("2.4.0"));
description.setLanguage(new JavaLanguage("11"));
description.addDependency("acme",
Dependency.withCoordinates("com.example", "acme").scope(DependencyScope.COMPILE));
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle.kts").containsExactly("plugins {",
" id(\"org.springframework.boot\") version \"2.1.0.RELEASE\"",
" id(\"io.spring.dependency-management\") version \"1.0.6.RELEASE\"", " java", "}", "",
"group = \"com.example\"", "version = \"0.0.1-SNAPSHOT\"",
"java.sourceCompatibility = JavaVersion.VERSION_11", "", "repositories {", " mavenCentral()", "}",
"", "dependencies {", " implementation(\"org.springframework.boot:spring-boot-starter\")",
assertThat(project).textFile("build.gradle.kts")
.containsExactly(// @formatter:off
"plugins {",
" id(\"org.springframework.boot\") version \"2.4.0\"",
" id(\"io.spring.dependency-management\") version \"1.0.6.RELEASE\"",
" java",
"}",
"",
"group = \"com.example\"",
"version = \"0.0.1-SNAPSHOT\"",
"java.sourceCompatibility = JavaVersion.VERSION_11",
"",
"repositories {",
" mavenCentral()",
"}",
"",
"dependencies {",
" implementation(\"org.springframework.boot:spring-boot-starter\")",
" implementation(\"com.example:acme\")",
" testImplementation(\"org.springframework.boot:spring-boot-starter-test\")", "}");
" testImplementation(\"org.springframework.boot:spring-boot-starter-test\")",
"}",
"",
"tasks.withType<Test> {",
" useJUnitPlatform()",
"}"); // @formatter:on
}

@Test
Expand Down Expand Up @@ -164,14 +181,4 @@ void junitPlatformIsConfiguredWithCompatibleVersion() {
" useJUnitPlatform()", "}");
}

@Test
void junitPlatformIsNotConfiguredWithIncompatibleVersion() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.1.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle.kts").lines().doesNotContainSequence("tasks.withType<Test> {",
" useJUnitPlatform()", "}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,36 @@ void gradleWrapperIsContributedWhenGeneratingGradleProject(String platformVersio
@Test
void buildDotGradleIsContributedWhenGeneratingGradleProject() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
description.setPlatformVersion(Version.parse("2.4.0"));
description.setLanguage(new JavaLanguage("11"));
description.addDependency("acme",
Dependency.withCoordinates("com.example", "acme").scope(DependencyScope.COMPILE));
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle").containsExactly("plugins {",
" id 'org.springframework.boot' version '2.1.0.RELEASE'",
" id 'io.spring.dependency-management' version '1.0.6.RELEASE'", " id 'java'", "}", "",
"group = 'com.example'", "version = '0.0.1-SNAPSHOT'", "sourceCompatibility = '11'", "",
"repositories {", " mavenCentral()", "}", "", "dependencies {",
assertThat(project).textFile("build.gradle").containsExactly(
// @formatter:off
"plugins {",
" id 'org.springframework.boot' version '2.4.0'",
" id 'io.spring.dependency-management' version '1.0.6.RELEASE'",
" id 'java'",
"}",
"",
"group = 'com.example'",
"version = '0.0.1-SNAPSHOT'",
"sourceCompatibility = '11'",
"",
"repositories {",
" mavenCentral()",
"}",
"",
"dependencies {",
" implementation 'org.springframework.boot:spring-boot-starter'",
" implementation 'com.example:acme'",
" testImplementation 'org.springframework.boot:spring-boot-starter-test'", "}");
" testImplementation 'org.springframework.boot:spring-boot-starter-test'",
"}",
"",
"test {",
" useJUnitPlatform()",
"}"); // @formatter:on
}

@Test
Expand All @@ -138,49 +155,17 @@ void junitPlatformIsConfiguredWithCompatibleVersion() {
assertThat(project).textFile("build.gradle").lines().containsSequence("test {", " useJUnitPlatform()", "}");
}

@Test
void junitPlatformIsNotConfiguredWithIncompatibleVersion() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.1.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle").lines().doesNotContainSequence("test {", " useJUnitPlatform()",
"}");
}

@Test
@Deprecated
void testStarterExcludesVintageEngineAndJUnitWithAppropriateVersion() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.2.0.M4"));
description.setLanguage(new JavaLanguage());
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle").lines().containsSequence(
" testImplementation('org.springframework.boot:spring-boot-starter-test') {",
" exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'",
" exclude group: 'junit', module: 'junit'", " }");
}

@Test
void testStarterExcludesVintageEngineWithCompatibleVersion() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.2.0.M5"));
description.setPlatformVersion(Version.parse("2.2.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle").lines().containsSequence(
" testImplementation('org.springframework.boot:spring-boot-starter-test') {",
" exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'", " }");
}

@Test
void testStarterDoesNotExcludesVintageEngineAndJUnitWithIncompatibleVersion() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse("2.1.6.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle").doesNotContain("exclude group");
}

@Test
void testStarterDoesNotExcludeVintageEngineWith24Snapshot() {
MutableProjectDescription description = new MutableProjectDescription();
Expand All @@ -199,19 +184,13 @@ void testStarterDoesNotExcludeVintageEngineWith24Milestone() {
assertThat(project).textFile("build.gradle").doesNotContain("exclude group");
}

static Stream<Arguments> annotationProcessorScopeBuildParameters() {
return Stream.of(Arguments.arguments("2.0.6.RELEASE", true), Arguments.arguments("2.1.3.RELEASE", true));
}

@ParameterizedTest(name = "Spring Boot {0}")
@MethodSource("annotationProcessorScopeBuildParameters")
void gradleAnnotationProcessorScopeCustomizerIsContributedIfNecessary(String platformVersion,
boolean contributorExpected) {
@Test
void gradleAnnotationProcessorScopeCustomizerIsContributed() {
MutableProjectDescription description = new MutableProjectDescription();
description.setPlatformVersion(Version.parse(platformVersion));
description.setPlatformVersion(Version.parse("2.4.0"));
description.setLanguage(new JavaLanguage());
this.projectTester.configure(description, (context) -> assertThat(context)
.getBeans(GradleAnnotationProcessorScopeBuildCustomizer.class).hasSize((contributorExpected) ? 1 : 0));
this.projectTester.configure(description,
(context) -> assertThat(context).hasSingleBean(GradleAnnotationProcessorScopeBuildCustomizer.class));
}

}
Loading

0 comments on commit b3c0f16

Please sign in to comment.