Spock import not resolving under buildSrc

Here’s what I have in my root project’s build.gradle:

plugins {
    id 'groovy'
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
    testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'
}

test {
    useJUnitPlatform()
}

and I have a test located at [root]/buildSrc/src/test/groovy/SpockTest.groovy that looks like:

import spock.lang.Specification

class SpockTest extends Specification {
    def "should be a simple assertion"() {
        expect:
        1 == 1
    }

}

When I build my project, I get an error:

startup failed: unable to resolve class spock.lang.Specification
 @ line 1, column 1.
   import spock.lang.Specification
   ^

If I remove the parent buildSrc folder and just have the test under [root]/src/test/groovy/SpockTest.groovy then it works just fine.

Is there a way for me to get my test working while keeping it under the buildSrc folder?

buildSrc is not a subproject of the root build, it is an “included build” (essentially its own root project). If you want to use spock in buildSrc, then it should have its own build.gradle with the appropriate repos/dependencies/configuration.

https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources

Ohhhh okay that makes more sense now - I didn’t realize I could add another build.gradle under buildSrc. I did just that and now it compiles, thank you! I’m having trouble running the test though:

root build.gradle (default setup when creating new project in IntelliJ):

plugins {
    id 'groovy'
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

buildSrc/build.gradle:

plugins {
    id 'groovy'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.spockframework:spock-core:2.0-groovy-2.5'
    testImplementation 'org.codehaus.groovy:groovy-all:2.5.4'
}

test {
    useJUnitPlatform()
}

buildSrc/src/test/groovy/SpockTest.groovy:

import spock.lang.Specification

class ExampleTest extends Specification{
    def "should be a simple assertion"() {
        expect:
        1 == 1
    }
}

When I try to run this test I get no errors but a message Test events were not received. After reading posts about similar issues, I tried modifying IntelliJ preferences to run tests using IntelliJ (instead of the default Gradle setting) and Invalidating Caches but I still get the same error. I’m on IntelliJ Community Edition 2019.3.3. Any thoughts here?

Also, would the command line equivalent be

./gradlew test --tests buildSrc/src/test/groovy/SpockTest.groovy 

? When I try it, I get a BUILD SUCCESSFUL message but no indication of any tests running

buildSrc is a special project that builds as part of Gradle’s startup. The intent of buildSrc is to hold common/custom build logic for the project. Gradle must build the buildSrc project before it executes the project’s buildscripts.

If you run any task with --console=verbose you’ll see how Gradle runs a build in buildSrc first, including the buildSrc:test task.