Skip to content

Commit 379a3eb

Browse files
committed
Include spring-boot-starter-test only if there's no other test starter
This change only affect Boot applications using 4.0.0-RC1 and later. Closes gh-1713
1 parent ac34099 commit 379a3eb

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/BuildProjectGenerationConfiguration.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import io.spring.initializr.generator.buildsystem.Build;
2020
import io.spring.initializr.generator.buildsystem.Dependency;
21-
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
2221
import io.spring.initializr.generator.buildsystem.DependencyScope;
2322
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
2423
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
@@ -48,11 +47,11 @@ public BuildCustomizer<Build> junit5TestStarterContributor() {
4847
.add("test",
4948
Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter-test")
5049
.scope(DependencyScope.TEST_COMPILE)
51-
.exclusions(new Exclusion("org.junit.vintage", "junit-vintage-engine")));
50+
.exclusions(new Dependency.Exclusion("org.junit.vintage", "junit-vintage-engine")));
5251
}
5352

5453
@Bean
55-
@ConditionalOnPlatformVersion("2.4.0-M1")
54+
@ConditionalOnPlatformVersion("[2.4.0-M1, 4.0.0-RC1)")
5655
public BuildCustomizer<Build> junitJupiterTestStarterContributor() {
5756
return (build) -> build.dependencies()
5857
.add("test", Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter-test")
@@ -67,6 +66,12 @@ BuildCustomizer<Build> junitLauncherContributor() {
6766
.scope(DependencyScope.TEST_RUNTIME));
6867
}
6968

69+
@Bean
70+
@ConditionalOnPlatformVersion("4.0.0-RC1")
71+
DefaultTestStarterBuildCustomizer defaultTestStarterBuildCustomizer() {
72+
return new DefaultTestStarterBuildCustomizer();
73+
}
74+
7075
@Bean
7176
public DefaultStarterBuildCustomizer defaultStarterContributor(InitializrMetadata metadata,
7277
ProjectDescription projectDescription) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012 - present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.build;
18+
19+
import io.spring.initializr.generator.buildsystem.Build;
20+
import io.spring.initializr.generator.buildsystem.Dependency;
21+
import io.spring.initializr.generator.buildsystem.DependencyScope;
22+
23+
import org.springframework.core.Ordered;
24+
25+
/**
26+
* A {@link BuildCustomizer} that adds the default test starter if none is detected.
27+
*
28+
* @author Moritz Halbritter
29+
*/
30+
class DefaultTestStarterBuildCustomizer implements BuildCustomizer<Build> {
31+
32+
private static final Dependency SPRING_BOOT_STARTER_TEST = Dependency
33+
.withCoordinates("org.springframework.boot", "spring-boot-starter-test")
34+
.scope(DependencyScope.TEST_COMPILE)
35+
.build();
36+
37+
/**
38+
* The id of the test starter to use if no dependency is defined.
39+
*/
40+
static final String STARTER_ID = "root_test_starter";
41+
42+
@Override
43+
public void customize(Build build) {
44+
boolean hasTestStarter = build.dependencies().items().anyMatch(this::isValidTestStarter);
45+
if (!hasTestStarter) {
46+
build.dependencies().add(STARTER_ID, SPRING_BOOT_STARTER_TEST);
47+
}
48+
}
49+
50+
@Override
51+
public int getOrder() {
52+
return Ordered.LOWEST_PRECEDENCE;
53+
}
54+
55+
private boolean isValidTestStarter(Dependency dependency) {
56+
boolean isTestStarter = dependency.getGroupId().equals("org.springframework.boot")
57+
&& dependency.getArtifactId().startsWith("spring-boot-starter-")
58+
&& dependency.getArtifactId().endsWith("-test");
59+
return isTestStarter && DependencyScope.TEST_COMPILE.equals(dependency.getScope());
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012 - present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.initializr.generator.spring.build;
18+
19+
import io.spring.initializr.generator.buildsystem.Build;
20+
import io.spring.initializr.generator.buildsystem.DependencyScope;
21+
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link DefaultTestStarterBuildCustomizer}.
28+
*
29+
* @author Moritz Halbritter
30+
*/
31+
class DefaultTestStarterBuildCustomizerTests {
32+
33+
@Test
34+
void defaultTestStarterIsAddedOnEmptyBuild() {
35+
Build build = new MavenBuild();
36+
new DefaultTestStarterBuildCustomizer().customize(build);
37+
assertThat(build.dependencies().ids()).containsOnly(DefaultTestStarterBuildCustomizer.STARTER_ID);
38+
}
39+
40+
@Test
41+
void defaultTestStarterIsAddedIfNoneExists() {
42+
Build build = new MavenBuild();
43+
build.dependencies().add("acme", "com.example", "acme", DependencyScope.COMPILE);
44+
new DefaultTestStarterBuildCustomizer().customize(build);
45+
assertThat(build.dependencies().ids()).containsOnly("acme", DefaultTestStarterBuildCustomizer.STARTER_ID);
46+
}
47+
48+
@Test
49+
void defaultTestStarterIsAddedIfNoTestScopedStarterExists() {
50+
Build build = new MavenBuild();
51+
build.dependencies()
52+
.add("kafka-test", "org.springframework.boot", "spring-boot-starter-kafka-test", DependencyScope.COMPILE);
53+
new DefaultTestStarterBuildCustomizer().customize(build);
54+
assertThat(build.dependencies().ids()).containsOnly("kafka-test", DefaultTestStarterBuildCustomizer.STARTER_ID);
55+
}
56+
57+
@Test
58+
void defaultTestStarterIsNotAddedIfTestScopedStarterExists() {
59+
Build build = new MavenBuild();
60+
build.dependencies()
61+
.add("kafka-test", "org.springframework.boot", "spring-boot-starter-kafka-test",
62+
DependencyScope.TEST_COMPILE);
63+
new DefaultTestStarterBuildCustomizer().customize(build);
64+
assertThat(build.dependencies().ids()).containsOnly("kafka-test");
65+
}
66+
67+
@Test
68+
void defaultTestStarterIsNotAddedIfDefaultTestScopedStarterExists() {
69+
Build build = new MavenBuild();
70+
build.dependencies()
71+
.add("test", "org.springframework.boot", "spring-boot-starter-test", DependencyScope.TEST_COMPILE);
72+
new DefaultTestStarterBuildCustomizer().customize(build);
73+
assertThat(build.dependencies().ids()).containsOnly("test");
74+
}
75+
76+
}

0 commit comments

Comments
 (0)