12

I am attempting to add Gradle (1.4) to an existing project that has multiple test suites. The standard unit test located in src/test/java ran successfully, but I am having trouble setting up a task to run the JUnit test located in src/integration-test/java.

When I run gradle intTest I get several cannot find symbol errors for classes in src/main. This leads me to believe that the dependencies are not set up correctly. How do I setup intTest so that it will run my JUnit integration tests?

build.gradle

apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_1_6

sourceSets {
    integration {
        java {
            srcDir 'src/integration-test/java'
        }
        resources {
            srcDir 'src/integration-test/resources'
        }
    }
}

dependencies {
    compile(group: 'org.springframework', name: 'spring', version: '3.0.7')

    testCompile(group: 'junit', name: 'junit', version: '4.+')
    testCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+')
    testCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+')
    testCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')

    integrationCompile(group: 'junit', name: 'junit', version: '4.+')
    integrationCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+')
    integrationCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+')
    integrationCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE')
}


task intTest(type: Test) {
    testClassesDir = sourceSets.integration.output.classesDir
    classpath += sourceSets.integration.runtimeClasspath
}

Details: Gradle 1.4

Solution: I had not set the compile classpath for the integration test source set (see below). In my I code I set the compile class path to sourceSets.test.runtimeClasspath so that I don't have the duplicate dependencies for "integrationCompile"

sourceSets {
    integrationTest {
        java {
            srcDir 'src/integration-test/java'
        }
        resources {
            srcDir 'src/integration-test/resources'
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}
Jason D
  • 8,023
  • 10
  • 33
  • 39
Rylander
  • 19,449
  • 25
  • 93
  • 144
  • Possible duplicate of [How do I add a new sourceset to Gradle?](http://stackoverflow.com/questions/11581419/how-do-i-add-a-new-sourceset-to-gradle) – childno͡.de Jun 23 '16 at 16:02

3 Answers3

9

the "integration" sourceSet has not configured its compile and runtime classpath. That's why it can't find the classes from your main sourceset. you can configure the compile and runtime classpath in the following way:

sourceSets {
    integTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
        compileClasspath = sourceSets.main.output + configurations.integTest
        runtimeClasspath = output + compileClasspath
    }
}
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • 1
    Thanks, I only needed to explicitly set the `compileClasspath`. The Runtime classpath is automatically setup as `classes + compileClasspath` – Rylander May 28 '13 at 17:30
  • 1
    In Gradle 1.6 you won't find `classes`. Use `output` instead. – Piohen Jun 11 '13 at 11:46
6

In most cases you want to use the same dependencies as your unit tests as well as some new ones. This will add the dependencies of your unit tests on top of the existing ones for integration tests (if any).

sourceSets {
    integrationTest {
        compileClasspath += sourceSets.test.compileClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath
    }
}
qwertzguy
  • 15,699
  • 9
  • 63
  • 66
  • Would you link some documentation to support this? Thanks – Rylander Aug 12 '14 at 17:33
  • @MikeRylander documentation about what in particular? If it's about the `+=` operator, you can see that `FileCollection` has a `plus` method to support overriding the `+` and `+=` operator: http://www.gradle.org/docs/current/javadoc/org/gradle/api/file/FileCollection.html – qwertzguy Aug 12 '14 at 19:02
  • I see that the integrationTest source set is extending the test classpath, but what is the second code block doing? – Rylander Sep 03 '14 at 16:00
  • @MikeRylander Ah, sorry that was not meant to be part of the post. Fixed. – qwertzguy Sep 03 '14 at 17:51
0

Another way:

test {
    exclude '**/*IntegrationTest*'
    ...
}

task testIntegration(type: Test) {
    include '**/*IntegrationTest*'
    ...
}
David
  • 4,191
  • 2
  • 31
  • 40