Skip to content

Commit f18554e

Browse files
committed
Overhaul Gradle section regarding dependency alignment (#4358)
Resolves #4343. --------- Co-authored-by: Sam Brannen <[email protected]> (cherry picked from commit 0068c24)
1 parent fa8c725 commit f18554e

File tree

1 file changed

+95
-35
lines changed

1 file changed

+95
-35
lines changed

Diff for: documentation/src/docs/asciidoc/user-guide/running-tests.adoc

+95-35
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ however, that it is recommended to use IDEA 2017.3 or newer since more recent ve
1313
IDEA download the following JARs automatically based on the API version used in the
1414
project: `junit-platform-launcher`, `junit-jupiter-engine`, and `junit-vintage-engine`.
1515

16-
WARNING: IntelliJ IDEA releases prior to IDEA 2017.3 bundle specific versions of JUnit 5.
17-
Thus, if you want to use a newer version of JUnit Jupiter, execution of tests within the
18-
IDE might fail due to version conflicts. In such cases, please follow the instructions
19-
below to use a newer version of JUnit 5 than the one bundled with IntelliJ IDEA.
20-
2116
In order to use a different JUnit 5 version (e.g., {jupiter-version}), you may need to
2217
include the corresponding versions of the `junit-platform-launcher`,
2318
`junit-jupiter-engine`, and `junit-vintage-engine` JARs in the classpath.
@@ -27,9 +22,7 @@ include the corresponding versions of the `junit-platform-launcher`,
2722
[subs=attributes+]
2823
----
2924
testImplementation(platform("org.junit:junit-bom:{bom-version}"))
30-
testRuntimeOnly("org.junit.platform:junit-platform-launcher") {
31-
because("Only needed to run tests in a version of IntelliJ IDEA that bundles older versions")
32-
}
25+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
3326
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
3427
testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
3528
----
@@ -40,7 +33,6 @@ testRuntimeOnly("org.junit.vintage:junit-vintage-engine")
4033
----
4134
<!-- ... -->
4235
<dependencies>
43-
<!-- Only needed to run tests in a version of IntelliJ IDEA that bundles older versions -->
4436
<dependency>
4537
<groupId>org.junit.platform</groupId>
4638
<artifactId>junit-platform-launcher</artifactId>
@@ -150,48 +142,63 @@ test {
150142
----
151143

152144
Please refer to the
153-
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_test[official Gradle documentation]
145+
https://docs.gradle.org/current/userguide/java_testing.html[official Gradle documentation]
154146
for a comprehensive list of options.
155147

156148
[[running-tests-build-gradle-bom]]
157149
===== Aligning dependency versions
158150

151+
TIP: See <<running-tests-build-spring-boot>> for details on how to override the version
152+
of JUnit used in your Spring Boot application.
153+
159154
Unless you're using Spring Boot which defines its own way of managing dependencies, it is
160-
recommended to use the JUnit Platform BOM to align the versions of all JUnit 5 artifacts.
155+
recommended to use the JUnit Platform <<dependency-metadata-junit-bom>> to align the
156+
versions of all JUnit 5 artifacts.
161157

162158
[source,groovy,indent=0]
163159
[subs=attributes+]
160+
.Explicit platform dependency on the BOM
164161
----
165162
dependencies {
166163
testImplementation(platform("org.junit:junit-bom:{bom-version}"))
164+
testImplementation("org.junit.jupiter:junit-jupiter")
165+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
167166
}
168167
----
169168

170169
Using the BOM allows you to omit the version when declaring dependencies on all artifacts
171170
with the `org.junit.platform`, `org.junit.jupiter`, and `org.junit.vintage` group IDs.
172171

173-
TIP: See <<running-tests-build-spring-boot>> for details on how to override the version
174-
of JUnit used in your Spring Boot application.
175-
176-
[[running-tests-build-gradle-config-params]]
177-
===== Configuration Parameters
178-
179-
The standard Gradle `test` task currently does not provide a dedicated DSL to set JUnit
180-
Platform <<running-tests-config-params, configuration parameters>> to influence test
181-
discovery and execution. However, you can provide configuration parameters within the
182-
build script via system properties (as shown below) or via the
183-
`junit-platform.properties` file.
172+
Since all JUnit artifacts declare a
173+
https://docs.gradle.org/current/userguide/platforms.html[platform] dependency on the BOM,
174+
you usually don't need to declare an explicit dependency on it yourself. Instead, it's
175+
sufficient to declare _one_ regular dependency that includes a version number. Gradle will
176+
then pull in the BOM automatically so you can omit the version for all other JUnit 5
177+
artifacts.
184178

185179
[source,groovy,indent=0]
180+
[subs=attributes+]
181+
.Implicit platform dependency on the BOM
186182
----
187-
test {
188-
// ...
189-
systemProperty("junit.jupiter.conditions.deactivate", "*")
190-
systemProperty("junit.jupiter.extensions.autodetection.enabled", true)
191-
systemProperty("junit.jupiter.testinstance.lifecycle.default", "per_class")
192-
// ...
183+
dependencies {
184+
testImplementation("org.junit.jupiter:junit-jupiter:{jupiter-version}") // <1>
185+
testRuntimeOnly("org.junit.platform:junit-platform-launcher") // <2>
193186
}
194187
----
188+
<1> Dependency declaration with explicit version. Pulls in the `junit-bom` automatically.
189+
<2> Dependency declaration without version. The version is supplied by the `junit-bom`.
190+
191+
[WARNING]
192+
.Declaring a dependency on junit-platform-launcher
193+
====
194+
Even though pre-8.0 versions of Gradle don't require declaring an explicit
195+
dependency on `junit-platform-launcher`, it is recommended to do so to ensure the versions
196+
of JUnit artifacts on the test runtime classpath are aligned.
197+
198+
Moreover, doing so is recommended and in some cases even required when importing the
199+
project into an IDE like <<running-tests-ide-eclipse>> or
200+
<<running-tests-ide-intellij-idea>>.
201+
====
195202

196203
[[running-tests-build-gradle-engines-configure]]
197204
===== Configuring Test Engines
@@ -205,7 +212,38 @@ on the dependency-aggregating JUnit Jupiter artifact similar to the following.
205212
[subs=attributes+]
206213
----
207214
dependencies {
208-
testImplementation("org.junit.jupiter:junit-jupiter:{jupiter-version}") // version can be omitted when using the BOM
215+
testImplementation("org.junit.jupiter:junit-jupiter:{jupiter-version}")
216+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
217+
}
218+
----
219+
220+
Alternatively, you can use Gradle's
221+
https://docs.gradle.org/current/userguide/jvm_test_suite_plugin.html[JVM Test Suite]
222+
support.
223+
224+
[source,kotlin,indent=0]
225+
[subs=attributes+]
226+
.Kotlin DSL
227+
----
228+
testing {
229+
suites {
230+
named<JvmTestSuite>("test") {
231+
useJUnitJupiter("{jupiter-version}")
232+
}
233+
}
234+
}
235+
----
236+
237+
[source,groovy,indent=0]
238+
[subs=attributes+]
239+
.Groovy DSL
240+
----
241+
testing {
242+
suites {
243+
test {
244+
useJUnitJupiter("{jupiter-version}")
245+
}
246+
}
209247
}
210248
----
211249

@@ -218,7 +256,28 @@ implementation similar to the following.
218256
----
219257
dependencies {
220258
testImplementation("junit:junit:{junit4-version}")
221-
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:{vintage-version}") // version can be omitted when using the BOM
259+
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:{vintage-version}")
260+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
261+
}
262+
----
263+
264+
[[running-tests-build-gradle-config-params]]
265+
===== Configuration Parameters
266+
267+
The standard Gradle `test` task currently does not provide a dedicated DSL to set JUnit
268+
Platform <<running-tests-config-params, configuration parameters>> to influence test
269+
discovery and execution. However, you can provide configuration parameters within the
270+
build script via system properties (as shown below) or via the
271+
`junit-platform.properties` file.
272+
273+
[source,groovy,indent=0]
274+
----
275+
test {
276+
// ...
277+
systemProperty("junit.jupiter.conditions.deactivate", "*")
278+
systemProperty("junit.jupiter.extensions.autodetection.enabled", true)
279+
systemProperty("junit.jupiter.testinstance.lifecycle.default", "per_class")
280+
// ...
222281
}
223282
----
224283

@@ -248,8 +307,8 @@ test {
248307

249308
Other logging frameworks provide different means to redirect messages logged using
250309
`java.util.logging`. For example, for {Logback} you can use the
251-
https://www.slf4j.org/legacy.html#jul-to-slf4j[JUL to SLF4J Bridge] by adding an
252-
additional dependency to the runtime classpath.
310+
https://www.slf4j.org/legacy.html#jul-to-slf4j[JUL to SLF4J Bridge] by adding it as a
311+
dependency to the test runtime classpath.
253312

254313
[[running-tests-build-maven]]
255314
==== Maven
@@ -288,7 +347,8 @@ Maven build as follows.
288347
===== Aligning dependency versions
289348

290349
Unless you're using Spring Boot which defines its own way of managing dependencies, it is
291-
recommended to use the JUnit Platform BOM to align the versions of all JUnit 5 artifacts.
350+
recommended to use the JUnit Platform <<dependency-metadata-junit-bom>> to align the
351+
versions of all JUnit 5 artifacts.
292352

293353
[source,xml,indent=0]
294354
[subs=attributes+]
@@ -580,8 +640,8 @@ managing the version of JUnit used in your project. In addition, the
580640
Jupiter, AssertJ, Mockito, etc.
581641

582642
If your build relies on dependency management support from Spring Boot, you should not
583-
import the <<dependency-metadata-junit-bom,`junit-bom`>> in your build script since that
584-
will result in duplicate (and potentially conflicting) management of JUnit dependencies.
643+
import JUnit's <<dependency-metadata-junit-bom>> in your build script since that would
644+
result in duplicate (and potentially conflicting) management of JUnit dependencies.
585645

586646
If you need to override the version of a dependency used in your Spring Boot application,
587647
you have to override the exact name of the

0 commit comments

Comments
 (0)