Skip to content

Commit bd65c13

Browse files
committed
Allow annotation processors to be made available to tests when using Gradle
Closes gh-1380
1 parent 4558dd9 commit bd65c13

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/DependencyScope.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
public enum DependencyScope {
2525

2626
/**
27-
* A dependency that is used as an annotation processor when compiling a project.
27+
* A dependency that is used as an annotation processor when compiling the main
28+
* sources of a project.
2829
*/
2930
ANNOTATION_PROCESSOR,
3031

@@ -61,6 +62,12 @@ public enum DependencyScope {
6162
/**
6263
* A dependency this is used to run a project's tests.
6364
*/
64-
TEST_RUNTIME
65+
TEST_RUNTIME,
66+
67+
/**
68+
* A dependency that is used as an annotation processor when compiling the test
69+
* sources of a project.
70+
*/
71+
TEST_ANNOTATION_PROCESSOR
6572

6673
}

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ private void writeDependencies(IndentingWriter writer, GradleBuild build) {
179179
sortedDependencies.addAll(filterDependencies(dependencies, hasScope(DependencyScope.TEST_COMPILE)));
180180
sortedDependencies.addAll(filterDependencies(dependencies, hasScope(DependencyScope.TEST_COMPILE_ONLY)));
181181
sortedDependencies.addAll(filterDependencies(dependencies, hasScope(DependencyScope.TEST_RUNTIME)));
182+
sortedDependencies
183+
.addAll(filterDependencies(dependencies, hasScope(DependencyScope.TEST_ANNOTATION_PROCESSOR)));
182184
if (!sortedDependencies.isEmpty()) {
183185
writer.println();
184186
writer.println("dependencies" + " {");
@@ -214,6 +216,7 @@ protected String configurationForDependency(Dependency dependency) {
214216
}
215217
return switch (type) {
216218
case ANNOTATION_PROCESSOR -> "annotationProcessor";
219+
case TEST_ANNOTATION_PROCESSOR -> "testAnnotationProcessor";
217220
case COMPILE -> "implementation";
218221
case COMPILE_ONLY -> "compileOnly";
219222
case PROVIDED_RUNTIME -> "providedRuntime";

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ private void writeDependencies(IndentingWriter writer, DependencyContainer depen
265265
}
266266
writeDependencies(writer, dependencies, hasScope(DependencyScope.RUNTIME));
267267
writeDependencies(writer, dependencies, hasScope(DependencyScope.COMPILE_ONLY));
268-
writeDependencies(writer, dependencies, hasScope(DependencyScope.ANNOTATION_PROCESSOR));
268+
writeDependencies(writer, dependencies,
269+
hasScope(DependencyScope.ANNOTATION_PROCESSOR, DependencyScope.TEST_ANNOTATION_PROCESSOR));
269270
writeDependencies(writer, dependencies, hasScope(DependencyScope.PROVIDED_RUNTIME));
270271
writeDependencies(writer, dependencies, hasScope(DependencyScope.TEST_COMPILE,
271272
DependencyScope.TEST_COMPILE_ONLY, DependencyScope.TEST_RUNTIME));
@@ -313,7 +314,7 @@ private void writeDependencyExclusion(IndentingWriter writer, Exclusion exclusio
313314
return null;
314315
}
315316
return switch (type) {
316-
case ANNOTATION_PROCESSOR, COMPILE, COMPILE_ONLY -> null;
317+
case ANNOTATION_PROCESSOR, TEST_ANNOTATION_PROCESSOR, COMPILE, COMPILE_ONLY -> null;
317318
case PROVIDED_RUNTIME -> "provided";
318319
case RUNTIME -> "runtime";
319320
case TEST_COMPILE, TEST_COMPILE_ONLY, TEST_RUNTIME -> "test";
@@ -325,6 +326,7 @@ private boolean isOptional(Dependency dependency) {
325326
return true;
326327
}
327328
return (dependency.getScope() == DependencyScope.ANNOTATION_PROCESSOR
329+
|| dependency.getScope() == DependencyScope.TEST_ANNOTATION_PROCESSOR
328330
|| dependency.getScope() == DependencyScope.COMPILE_ONLY);
329331
}
330332

initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ void gradleBuildWithAnnotationProcessorDependency() {
360360
}""");
361361
}
362362

363+
@Test
364+
void gradleBuildWithTestAnnotationProcessorDependency() {
365+
GradleBuild build = new GradleBuild();
366+
build.dependencies()
367+
.add("test-annotation-processor", "org.springframework.boot", "spring-boot-configuration-processor",
368+
DependencyScope.TEST_ANNOTATION_PROCESSOR);
369+
assertThat(write(build)).contains("""
370+
dependencies {
371+
testAnnotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
372+
}""");
373+
}
374+
363375
@Test
364376
void gradleBuildWithCompileDependency() {
365377
GradleBuild build = new GradleBuild();

initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ void gradleBuildWithAnnotationProcessorDependency() {
364364
}""");
365365
}
366366

367+
@Test
368+
void gradleBuildWithTestAnnotationProcessorDependency() {
369+
GradleBuild build = new GradleBuild();
370+
build.dependencies()
371+
.add("test-annotation-processor", "org.springframework.boot", "spring-boot-configuration-processor",
372+
DependencyScope.TEST_ANNOTATION_PROCESSOR);
373+
assertThat(write(build)).contains("""
374+
dependencies {
375+
testAnnotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
376+
}""");
377+
}
378+
367379
@Test
368380
void gradleBuildWithCompileDependency() {
369381
GradleBuild build = new GradleBuild();

initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,23 @@ void pomWithAnnotationProcessorDependency() {
313313
});
314314
}
315315

316+
@Test
317+
void pomWithTestAnnotationProcessorDependency() {
318+
MavenBuild build = new MavenBuild();
319+
build.settings().coordinates("com.example.demo", "demo");
320+
build.dependencies()
321+
.add("test-annotation-processor", "org.springframework.boot", "spring-boot-configuration-processor",
322+
DependencyScope.TEST_ANNOTATION_PROCESSOR);
323+
generatePom(build, (pom) -> {
324+
NodeAssert dependency = pom.nodeAtPath("/project/dependencies/dependency");
325+
assertThat(dependency).textAtPath("groupId").isEqualTo("org.springframework.boot");
326+
assertThat(dependency).textAtPath("artifactId").isEqualTo("spring-boot-configuration-processor");
327+
assertThat(dependency).textAtPath("version").isNullOrEmpty();
328+
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
329+
assertThat(dependency).textAtPath("optional").isEqualTo("true");
330+
});
331+
}
332+
316333
@Test
317334
void pomWithCompileOnlyDependency() {
318335
MavenBuild build = new MavenBuild();

0 commit comments

Comments
 (0)