How to use generated test sources in a Test task?


(thirsch81) #1

Hello,

I’m trying to emulate basic Fitnesse behaviour based on Spock/Geb. I have a problem when running the tests:

Wiki.WikiTest > initializationError FAILED >

java.lang.ClassNotFoundException: Wiki.WikiTest

Here’s the code that probably contains the error:

}configurations {
    wikiTest.extendsFrom compile
    wikiTest.each { println it.name }
}
  sourceSets {
    wikiTest { groovy.srcDirs 'src/test/generated' }
}
  def codeGeneratingCopySpec = copySpec {
    from 'src/test/resources'
    include 'generic-spec.groovy.template'
    rename '.*', 'WikiTest.groovy'
}
  task generateGroovyCodeFromWikiSpec(dependsOn: gatherSingleSpecFromWiki) {
    ext { spec = null }
    doFirst {
        spec = gatherSingleSpecFromWiki.spec
        codeGeneratingCopySpec.expand 'testMethodContent': spec
    }
    doLast {
        copy {
            with codeGeneratingCopySpec
            into 'src/test/generated/Wiki'
        }
    }
    compileWikiTestGroovy.groovyClasspath = compileGroovy.groovyClasspath
    compileWikiTestGroovy.classpath = configurations.testCompile + sourceSets.main.output
    compileWikiTestGroovy.dependsOn it
}
  task wikiTest(type: Test) {
    dependsOn compileWikiTestGroovy
    testClassesDir
= sourceSets.wikiTest.output.classesDir
    testSrcDirs
= sourceSets.wikiTest.groovy.srcDirs as List<File>
}

The code does get compiled however, and the outputDir is

build/classes/wikiTest

as expected


(Peter Niederwieser) #2

The ‘wikiTest’ task configuration is missing something like ‘classpath = sourceSets.wikiTest.runtimeClasspath’. Instead of configuring the class paths of the ‘compileWikiTestGroovy’ task (which, by the way, shouldn’t happen inside the configuration block for ‘generateGroovyCodeFromWikiSpec’), it would be better to configure ‘sourceSets.wikiTest.compileClasspath’ and ‘sourceSets.wikiTest.runtimeClasspath’.


(thirsch81) #3

I was also thinking about where to put the configuration of the

compileWikiTestGroovy

task, and I was thinking, it would be ok this way, since compilation would not make sense at all without the dependent task. I also figured I would likely be removing it anways, once I know the correct solution, that I felt must have be about configuring the sourceSets correctly. :wink:

But thanks for pointing it out, and for making Gradle and providing exceptional support with it!