Clarification on spock test integrations

I’ve been having a great deal of trouble setting up spock to test some groovy classes in my gradle build.

First let me explain the groovy files. I needed the build to run slightly different based on which linux OS it was installed against. The gradle build script determines which OS is running at startup and instantiates a groovy object specific to that OS. I created generic tasks in the gradle.build file that would call methods on the instantiated objects.

I put all the groovy class files in /buildSrc/src/main/groovy/ under the project, and everything just worked.

Recently I’ve tried to add some Spock unit tests as due diligence. When placed in /buildSrc/test/main/groovy I keep getting “unable to resolve class spock.lang.Specification” despite importing spock.lang.* in the spec files, and included the framework through the dependencies in build.gradle.

Is there something simple I could be missing? Is there a good tutorial project for this kind of gradle layout?

buildSrc can be thought of as it’s own project with its own build.gradle (buildSrc/build.gradle). Did you add the spock framework dependencies to buildSrc/build.gradle?

An example of what one of my buildSrc/build.gradle files looks like:

apply plugin: 'groovy'
repositories {
    // ... internal artifactory repos
}
dependencies {
    compile ...
    testCompile( group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4') {
        exclude module: 'groovy-all'
    }
}

The user guide has some content on buildSrc. https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:build_sources
EDIT: I didn’t realize this until just reading it now, “Gradle applies the default build script regardless of whether there is one specified.” Therefore I don’t need to apply the groovy plugin in my code. Always good when you learn something trying to help others :slight_smile:

1 Like

Thanks! That worked.

Although I’ll note for posterity, I only needed the testCompile line in dependencies. I suppose since I’m not doing anything too tricky.