Skip to content

Commit 72600e4

Browse files
authored
Build/testing: add java-test-fixtures to every Java project & reduce repeated dependency declarations (#714)
Reduce the amount of dependency declarations in all projects by adding adding the dependencies logback-classic (runtimeOnly) and assertj+mockito (implementation) to all test sources. test-fixtures get junit-jupiter+assertj+mockito as implementation dependencies. Also ensures that `useJUnitJupiter` is called with the correct version (it implicitly adds the jupiter runtime). logback-classic is needed at test runtime to actually get logs during test executions, logback-core is a dependency of logback-classic that provides the logback implementation.
1 parent f1dfc97 commit 72600e4

File tree

9 files changed

+74
-60
lines changed

9 files changed

+74
-60
lines changed

build-logic/src/main/kotlin/polaris-java.gradle.kts

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import org.gradle.kotlin.dsl.named
2626
import publishing.PublishingHelperPlugin
2727

2828
plugins {
29-
id("jacoco")
30-
id("java")
29+
jacoco
30+
`java-library`
31+
`java-test-fixtures`
32+
`jvm-test-suite`
3133
id("com.diffplug.spotless")
3234
id("jacoco-report-aggregation")
3335
id("net.ltgt.errorprone")
@@ -63,9 +65,69 @@ tasks.register("format").configure {
6365
dependsOn("spotlessApply")
6466
}
6567

66-
tasks.named<Test>("test").configure {
67-
useJUnitPlatform()
68-
jvmArgs("-Duser.language=en")
68+
tasks.named<Test>("test").configure { jvmArgs("-Duser.language=en") }
69+
70+
testing {
71+
suites {
72+
withType<JvmTestSuite> {
73+
val libs = versionCatalogs.named("libs")
74+
75+
useJUnitJupiter(
76+
libs
77+
.findLibrary("junit-bom")
78+
.orElseThrow { GradleException("junit-bom not declared in libs.versions.toml") }
79+
.map { it.version!! }
80+
)
81+
82+
dependencies {
83+
implementation(project())
84+
implementation(testFixtures(project()))
85+
runtimeOnly(
86+
libs.findLibrary("logback-classic").orElseThrow {
87+
GradleException("logback-classic not declared in libs.versions.toml")
88+
}
89+
)
90+
implementation(
91+
libs.findLibrary("assertj-core").orElseThrow {
92+
GradleException("assertj-core not declared in libs.versions.toml")
93+
}
94+
)
95+
implementation(
96+
libs.findLibrary("mockito-core").orElseThrow {
97+
GradleException("mockito-core not declared in libs.versions.toml")
98+
}
99+
)
100+
}
101+
102+
targets.all {
103+
if (testTask.name != "test") {
104+
testTask.configure { shouldRunAfter("test") }
105+
}
106+
}
107+
}
108+
}
109+
}
110+
111+
dependencies {
112+
val libs = versionCatalogs.named("libs")
113+
testFixturesImplementation(
114+
platform(
115+
libs.findLibrary("junit-bom").orElseThrow {
116+
GradleException("junit-bom not declared in libs.versions.toml")
117+
}
118+
)
119+
)
120+
testFixturesImplementation("org.junit.jupiter:junit-jupiter")
121+
testFixturesImplementation(
122+
libs.findLibrary("assertj-core").orElseThrow {
123+
GradleException("assertj-core not declared in libs.versions.toml")
124+
}
125+
)
126+
testFixturesImplementation(
127+
libs.findLibrary("mockito-core").orElseThrow {
128+
GradleException("mockito-core not declared in libs.versions.toml")
129+
}
130+
)
69131
}
70132

71133
tasks.withType(Jar::class).configureEach {

dropwizard/service/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ dependencies {
125125

126126
testCompileOnly(libs.smallrye.common.annotation)
127127

128-
testImplementation(platform(libs.junit.bom))
129-
testImplementation("org.junit.jupiter:junit-jupiter")
130-
testImplementation(libs.assertj.core)
131-
testImplementation(libs.mockito.core)
132-
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
133-
134128
testImplementation(project(":polaris-eclipselink"))
135129
}
136130

extension/persistence/eclipselink/build.gradle.kts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ fun isValidDep(dep: String): Boolean {
2222
return dep.matches(depRegex)
2323
}
2424

25-
plugins {
26-
id("polaris-server")
27-
`java-library`
28-
}
25+
plugins { id("polaris-server") }
2926

3027
dependencies {
3128
implementation(project(":polaris-core"))
@@ -52,12 +49,6 @@ dependencies {
5249

5350
testImplementation(libs.h2)
5451
testImplementation(testFixtures(project(":polaris-core")))
55-
56-
testImplementation(platform(libs.junit.bom))
57-
testImplementation("org.junit.jupiter:junit-jupiter")
58-
testImplementation(libs.assertj.core)
59-
testImplementation(libs.mockito.core)
60-
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
6152
}
6253

6354
tasks.register<Jar>("createTestConfJar") {

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jakarta-ws-rs-api = { module = "jakarta.ws.rs:jakarta.ws.rs-api", version = "4.0
6464
javax-annotation-api = { module = "javax.annotation:javax.annotation-api", version = "1.3.2" }
6565
jetbrains-annotations = { module = "org.jetbrains:annotations", version = "26.0.1" }
6666
junit-bom = { module = "org.junit:junit-bom", version = "5.11.4" }
67-
logback-core = { module = "ch.qos.logback:logback-core", version = "1.4.14" }
67+
logback-classic = { module = "ch.qos.logback:logback-classic", version = "1.4.14" }
6868
micrometer-bom = { module = "io.micrometer:micrometer-bom", version = "1.14.3" }
6969
mockito-core = { module = "org.mockito:mockito-core", version = "5.15.2" }
7070
opentelemetry-bom = { module = "io.opentelemetry:opentelemetry-bom", version = "1.46.0" }

polaris-core/build.gradle.kts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
plugins {
21-
id("polaris-client")
22-
id("java-library")
23-
id("java-test-fixtures")
24-
}
20+
plugins { id("polaris-client") }
2521

2622
dependencies {
2723
implementation(project(":polaris-api-management-model"))
@@ -95,10 +91,6 @@ dependencies {
9591
implementation(platform(libs.google.cloud.storage.bom))
9692
implementation("com.google.cloud:google-cloud-storage")
9793

98-
testFixturesApi(platform(libs.junit.bom))
99-
testFixturesApi("org.junit.jupiter:junit-jupiter")
100-
testFixturesApi(libs.assertj.core)
101-
testFixturesApi(libs.mockito.core)
10294
testFixturesApi("com.fasterxml.jackson.core:jackson-core")
10395
testFixturesApi("com.fasterxml.jackson.core:jackson-databind")
10496
testFixturesApi(libs.commons.lang3)

service/common/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ dependencies {
6868

6969
implementation(libs.auth0.jwt)
7070

71-
implementation(libs.logback.core)
7271
implementation(libs.bouncycastle.bcprov)
7372

7473
implementation(platform(libs.google.cloud.storage.bom))

tools/config-docs/annotations/build.gradle.kts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
* under the License.
1818
*/
1919

20-
plugins {
21-
id("polaris-client")
22-
`java-library`
23-
}
20+
plugins { id("polaris-client") }
2421

2522
description = "Polaris reference docs annotations"

tools/config-docs/generator/build.gradle.kts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
plugins {
21-
id("polaris-server")
22-
`java-library`
23-
`java-test-fixtures`
24-
}
20+
plugins { id("polaris-server") }
2521

2622
description = "Generates Polaris reference docs"
2723

@@ -36,12 +32,6 @@ dependencies {
3632
implementation(libs.picocli)
3733
annotationProcessor(libs.picocli.codegen)
3834

39-
testFixturesApi(platform(libs.junit.bom))
40-
testFixturesApi("org.junit.jupiter:junit-jupiter")
41-
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
42-
testFixturesApi(libs.assertj.core)
43-
testFixturesApi(libs.mockito.core)
44-
4535
genTesting(project(":polaris-config-docs-annotations"))
4636
genTesting(libs.smallrye.config.core)
4737
}

tools/version/build.gradle.kts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@
1919

2020
import org.apache.tools.ant.filters.ReplaceTokens
2121

22-
plugins {
23-
id("polaris-client")
24-
`java-library`
25-
`java-test-fixtures`
26-
`jvm-test-suite`
27-
}
22+
plugins { id("polaris-client") }
2823

2924
dependencies { testFixturesApi(libs.assertj.core) }
3025

@@ -81,14 +76,8 @@ val jarTestJar by
8176
// need to test the `jar:` scheme/protocol resolution.
8277
testing {
8378
suites {
84-
withType<JvmTestSuite> { useJUnitJupiter(libs.junit.bom.map { it.version!! }) }
85-
8679
register<JvmTestSuite>("jarTest") {
87-
dependencies {
88-
compileOnly(project())
89-
runtimeOnly(files(jarTestJar.get().archiveFile.get().asFile))
90-
implementation(libs.assertj.core)
91-
}
80+
dependencies { runtimeOnly(files(jarTestJar.get().archiveFile.get().asFile)) }
9281

9382
targets.all {
9483
testTask.configure {

0 commit comments

Comments
 (0)