How to run Spock tests for Java project with eclipse and gradle?

I’m facing troubles with this test execution. I’ve followed this tutorial: in order to configure my project. But when I run I got the following exception:

Class not found spock.tests.CustomerReaderSpec
java.lang.ClassNotFoundException: spock.tests.CustomerReaderSpec at at
java.lang.ClassLoader.loadClass( at
sun.misc.Launcher$AppClassLoader.loadClass( at

I don’t know why, because the class is under the following folder, as you can see:

enter image description here

Here’s my build.gradle:

apply plugin: 'groovy'
apply plugin: 'eclipse'

repositories {

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.0.5'

    implementation ''

    compile group: '', name: 'spring-data-jpa', version: '2.0.2.RELEASE'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.1.Final'



task runScript (dependsOn: 'classes', type: JavaExec) {
    main = 'myscript'
    classpath = sourceSets.main.runtimeClasspath

test {
    useJUnit {
        includeCategories 'spock.tests.UnitTest'
    testLogging {
        showStandardStreams = true

What could be wrong with it?

One thought on “How to run Spock tests for Java project with eclipse and gradle?”

  1. 2 problems I can see in your sample project:
    1) CustomerReaderSpec is missing “package spock.tests” at the top
    2) Dependency “org.springframework:spring-test:5.0.2.RELEASE” is needed in testCompile
    Also, be sure the Groovy compiler you have selected for your workspace (Window > Preferences > Groovy > Compiler) matches — at least major and minor — the version of groovy-all dependency you are using.
    With those changes, I was able to run the spock test.

Leave a Reply

Your email address will not be published.