Search code examples
buildgradlebuild-automationdependency-managementcucumber-junit

Gradle does not download dependencies for tests defined in custom sourceSet


I have the following project structure:

MyProject
   --src
   --test
      --acceptance
         --java
         --resources
      --unit

and the following build.gradle

apply plugin: 'java'
apply plugin: 'idea'

version = '0.1'

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
    unit {
        java {
            srcDir 'test/unit'
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
    acceptance {
        java {
            srcDir 'test/acceptance/java'
        }
        resources {
            srcDir 'test/acceptance/resources'
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}

dependencies {
    compile fileTree(dir: 'lib', include: '*.jar')

    unitCompile group: 'junit', name: 'junit', version: '4.11'

    acceptanceCompile group: 'junit', name: 'junit', version: '4.11'
    acceptanceCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.1.3'
    acceptanceCompile group: 'info.cukes', name: 'cucumber-java', version: '1.1.3'
}


task unitTest(type: Test) {
    description = "Runs Unit Tests"
    classpath += sourceSets.unit.runtimeClasspath
    testClassesDir = sourceSets.unit.output.classesDir
}


task acceptanceTest(type: Test) {
    description = "Runs Acceptance Tests"
    classpath += sourceSets.acceptance.runtimeClasspath
    testClassesDir = sourceSets.acceptance.output.classesDir
}

but running 'gradle acceptanceTest' leads to compilation issues as it looks like the dependencies are not found on the classpath.


Solution

  • It looks like in version 1.1.3, the Cucumber class is in package

    cucumber.api.junit
    

    rather than

    cucumber.junit
    

    Changing the import statement seems to have resolved the classpath issue at least.

    However, there are issues with running Cucumber JUnit tests through gradle. Further information, and a workaround using a javaexec task is given here: https://github.com/yatskevich/cucumber-jvm-gradle-javaexec. In summary, with the below task:

      task cucumber() {
        dependsOn assemble, compileAcceptanceJava
        doLast {
            javaexec {
                main = "cucumber.api.cli.Main"
                classpath = configurations.cucumberRuntime + sourceSets.main.output +
                        sourceSets.acceptance.output + sourceSets.unit.runtimeClasspath +
                        sourceSets.acceptance.runtimeClasspath
    
                args = ['-f', 'junit:build/reports/test-results/cucumber.xml',
                        '-f', 'html:build/reports/test/cucumber-tests.html',
                        '--glue', 'com.paragon', 'test/acceptance/features']
            }
        }
    }
    

    we can now run

    gradle cucumber
    

    and generate the JUnit report.