zizhuo64062kw
zizhuo64062kw

Reputation: 421

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?

Upvotes: 28

Views: 34397

Answers (7)

Kramer
Kramer

Reputation: 1058

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

Upvotes: 0

Topera
Topera

Reputation: 12389

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

Upvotes: 0

bhagyas
bhagyas

Reputation: 3080

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.

Upvotes: 3

ruseel
ruseel

Reputation: 1734

this works for me (in Gradle 5.6)

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

Upvotes: 2

vdshb
vdshb

Reputation: 1999

Worked for me (Gradle 6.3, Kotlin DSL):

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

Upvotes: 3

specialk1st
specialk1st

Reputation: 1727

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

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

Upvotes: 3

Peter Niederwieser
Peter Niederwieser

Reputation: 123890

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

Upvotes: 38

Related Questions