Gradle Goodness: Add Extra Dependencies For Running Tests Using JVM Test Suite Plugin
The JVM Test Suite plugin adds an extension to our build that allows us to configure test tasks. We always can access the default test
task and for example specify the test framework we want to use. Gradle will then automatically add the dependencies of that test framework to the testImplementation
configuration. If we want to add more dependencies to the testImplementation
configuration we don’t have to do that by explicitly mentioning the testImplementation
configuration. Instead we can also use a dependencies
block from within the JvmTestSuite
extension. Any extra dependencies we need to run our tests can be added using the configuration names without a test
prefix. Gradle will automatically add them to the correct test configuration for us so the dependencies are available when we compile and run our tests. This will also work for any other new test type we add to the test suites, e.g. for integration tests.
In the following example Gradle build file we configure two extra dependencies for our test related tasks. We want to use AssertJ and Datafaker in our tests so we add them as dependencies:
plugins {
java
}
repositories {
mavenCentral()
}
// Using JVM Test Suite feature to configure our test task.
testing {
suites {
val test by getting(JvmTestSuite::class) {
// We set the Junit version explicit using the version catalog.
useJUnitJupiter(libs.versions.junit)
dependencies {
// We add extra dependencies needed for our tests.
// We don't use the test prefix here as we are already
// in a "test" context.
// Other configurations like annotationProcessor, compileOnly
// can be used here as well.
// Here we refer to dependencies using the version catalog,
// but we could use string values for example.
implementation(libs.assertj.core)
implementation(libs.datafaker)
}
}
}
}
The version catalog looks as follows:
# File: gradle/libs.versions.toml
[versions]
junit = "5.9.1"
[libraries]
assertj-core = "org.assertj:assertj-core:3.23.1"
datafaker = "net.datafaker:datafaker:1.6.0"
We can use the dependencies
task to see what our test runtime classpath looks like. And we see all our newly defined dependencies as well:
$ gw dependencies --configuration testRuntimeClasspath
> Task :dependencies
------------------------------------------------------------
Root project 'testsuite'
------------------------------------------------------------
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- org.junit.jupiter:junit-jupiter:5.9.1
| +--- org.junit:junit-bom:5.9.1
| | +--- org.junit.jupiter:junit-jupiter:5.9.1 (c)
| | +--- org.junit.jupiter:junit-jupiter-api:5.9.1 (c)
| | +--- org.junit.jupiter:junit-jupiter-engine:5.9.1 (c)
| | +--- org.junit.jupiter:junit-jupiter-params:5.9.1 (c)
| | +--- org.junit.platform:junit-platform-commons:1.9.1 (c)
| | \--- org.junit.platform:junit-platform-engine:1.9.1 (c)
| +--- org.junit.jupiter:junit-jupiter-api:5.9.1
| | +--- org.junit:junit-bom:5.9.1 (*)
| | +--- org.opentest4j:opentest4j:1.2.0
| | \--- org.junit.platform:junit-platform-commons:1.9.1
| | \--- org.junit:junit-bom:5.9.1 (*)
| +--- org.junit.jupiter:junit-jupiter-params:5.9.1
| | +--- org.junit:junit-bom:5.9.1 (*)
| | \--- org.junit.jupiter:junit-jupiter-api:5.9.1 (*)
| \--- org.junit.jupiter:junit-jupiter-engine:5.9.1
| +--- org.junit:junit-bom:5.9.1 (*)
| +--- org.junit.platform:junit-platform-engine:1.9.1
| | +--- org.junit:junit-bom:5.9.1 (*)
| | +--- org.opentest4j:opentest4j:1.2.0
| | \--- org.junit.platform:junit-platform-commons:1.9.1 (*)
| \--- org.junit.jupiter:junit-jupiter-api:5.9.1 (*)
+--- org.assertj:assertj-core:3.23.1
| \--- net.bytebuddy:byte-buddy:1.12.10
\--- net.datafaker:datafaker:1.6.0
\--- com.github.mifmif:generex:1.0.2
\--- dk.brics.automaton:automaton:1.11-8
(c) - dependency constraint
(*) - dependencies omitted (listed previously)
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 709ms
1 actionable task: 1 executed
$
Written with Gradle 7.6.