'How to list classpath for tests in Gradle
When I try running gradle test, I get the following output:
$ gradle test
:ro:compileJava UP-TO-DATE
:ro:processResources UP-TO-DATE
:ro:classes UP-TO-DATE
:ro:jar
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
ro.idea.ToggleTest > testIsAd FAILED
java.lang.NoClassDefFoundError at ToggleTest.java:13
Caused by: java.lang.ClassNotFoundException at ToggleTest.java:13
ro.idea.ToggleTest > testToggle FAILED
java.lang.NoClassDefFoundError at ToggleTest.java:13
2 tests completed, 2 failed
:test FAILED
So I want to check my classpath to see whether my classpath is wrong or not.
My question is: How can I list the classpath at test time with a Gradle task?
Solution 1:[1]
You can list test runtime dependencies with:
gradle dependencies --configuration=testRuntime
Or, if you want to see the actual files:
task printClasspath {
doLast {
configurations.testRuntime.each { println it }
}
}
Solution 2:[2]
Also, to list the classpath for the main (non-test) application run use:
run << {
doLast {
configurations.runtime.each { println it }
}
}
Solution 3:[3]
Worked for me (Gradle 6.3, Kotlin DSL):
tasks.withType<Test> {
this.classpath.forEach { println(it) }
}
Solution 4:[4]
this works for me (in Gradle 5.6)
task printTestClasspath.doLast {
println(sourceSets.test.runtimeClasspath.asPath)
}
Solution 5:[5]
Assuming you are using a Gradle wrapper, you can use the following.
./gradlew dependencies --configuration=testRuntimeClasspath
It will list the dependencies as available to your tests.
Solution 6:[6]
Expanding solution of Peter Niederwieser, if you want to print from all possible configurations (using the simple loop) and ignoring possible errors:
task printClasspath {
doLast {
configurations.each { Configuration configuration ->
try {
println configuration
configuration.each { println it }
} catch (Exception e){
println "Error getting details of $configuration"
}
}
}
}
Solution 7:[7]
I'm using gradle 7.3.3. and this worked for me:
tasks.withType<Test> {
useJUnitPlatform()
println("Test classpath")
sourceSets.test.get().runtimeClasspath.forEach { println(it) }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Niederwieser |
| Solution 2 | specialk1st |
| Solution 3 | |
| Solution 4 | ruseel |
| Solution 5 | bhagyas |
| Solution 6 | Topera |
| Solution 7 | Kramer |
