Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an additional test suite to Gradle

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
    }
}
like image 347
Mike Rylander Avatar asked May 24 '13 18:05

Mike Rylander


People also ask

How do you run a parallel test in Gradle?

TL;DR: When you set maxParallelForks in Gradle to run tests, the test classes run in parallel, but the individual tests in a single class do not. One of the best features in Gradle for JVM-related projects is its ability to run tests in parallel.

Does Gradle build include test?

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.


1 Answers

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
    }
}
like image 98
Rene Groeschke Avatar answered Oct 18 '22 06:10

Rene Groeschke