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?


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 }
    }
}

Also, to list the classpath for the main (non-test) application run use:

run << {
    doLast {
        configurations.runtime.each { println it }
    }
}

Worked for me (Gradle 6.3, Kotlin DSL):

tasks.withType<Test> {
    this.classpath.forEach { println(it) }
}

this works for me (in Gradle 5.6)

task printTestClasspath.doLast {
  println(sourceSets.test.runtimeClasspath.asPath)
}