pulp
pulp

Reputation: 708

Gradle (java): test task should use generated .jar and not the .class files in classpath

Gradle with apply plugin: 'java' in build.gradle. The file will create a .jar file and the test task is running junit tests:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
  testCompile 'junit:junit:4.12'
}

This is working. But to make sure the public API tests are working with the generated .jar file I want that the 'test' task is running the test with the generated .jar file from the build/libs folder in classpath and not with the generate .class files from folder build/classes in in classpath.

Not working because the sourceSets is set global:

tasks.withType(Test) {
    sourceSets {
        main {
            java {
                exclude '**'
            }
        }
    }
}

Partly working: multiproject (test and jar separated in two gradle projects):

dependencies {
  compile project(":jar_project")
  testCompile 'junit:junit:4.12'
}

in this case jar_project.jar is used but package private test are also executed without an error.

Do somebody have an idea how to run the tests with the .jar as dependency and ignoring the .class files?

Thank you, pulp

Upvotes: 4

Views: 2005

Answers (1)

smac89
smac89

Reputation: 43078

The problem is that the test task does not depend on the jar task, which sort of makes sense because the jar is supposed to package the classes, so there should be no reason for tests to depend on that jar.

You can force the dependency by:

test {
  dependsOn jar
  doFirst {
    classpath += jar.outputs.files
  }
}

Now the jar will be on the test classpath

Upvotes: 0

Related Questions