Blazerg
Blazerg

Reputation: 849

How to configure spock in spring boot with gradle 6+ and java 11+

I want to use spock in my spring-boot project (using groovy, not java). I have some projects with spock already working in java 8 and lower versions of gradle but I can't find documentation about the newest versions.

This is my OLD build.gradle

    buildscript {
    ext {
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.codehaus.groovy:groovy')
    testImplementation('org.springframework.boot:spring-boot-starter-test')

    testCompile(
            'junit:junit:4.12',
            'org.codehaus.groovy:groovy-all:2.4.4',
            'org.spockframework:spock-core:1.2-groovy-2.4',
            'cglib:cglib:2.2',
            'org.spockframework:spock-spring:1.2-groovy-2.4',
    )
}

this works and I can create spock tests but when I create a project with java 11 and gradle 6.4, nothing works anymore (the gradle syntax is way different and the spock libraries don't work anymore),

this is my CURRENT (and failing) build.gradle:

plugins {
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'groovy'
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.codehaus.groovy:groovy'
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'org.postgresql:postgresql'


    testImplementation("org.spockframework:spock-core") {
        exclude group: "org.codehaus.groovy", module: "groovy-all"

    }
    testImplementation group: 'org.spockframework', name: 'spock-spring', version: '2.0-M3-groovy-3.0'
    testImplementation(enforcedPlatform("org.spockframework:spock-bom:2.0-M1-groovy-2.5"))
}

test {
    useJUnitPlatform()
}

With this, nothing works but the gradle build doesn't found the spock-spring libraries.

How can I apply the spock libraries to spring boot with the newest versions of java and gradle?

Upvotes: 4

Views: 8083

Answers (1)

tim_yates
tim_yates

Reputation: 171084

Think you got into a muddle of Groovy Versions with the dependencies:

I tested with this build:

plugins {
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'groovy'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
    mavenCentral()
    jcenter()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.codehaus.groovy:groovy:3.0.5'
    testImplementation('org.spockframework:spock-core:2.0-M3-groovy-3.0')
    testImplementation('org.spockframework:spock-spring:2.0-M3-groovy-3.0')
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

And this test ran and passed:

package test

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.ApplicationContext
import spock.lang.Specification

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Application)
class ContextSpec extends Specification {

    @Autowired
    ApplicationContext context

    def "context is as expected"() {
        expect:
        context
        context.getBean("echoService")
    }
}

(Obviously, I had an echoService in my context)

For completeness, here's the service:

package test

import org.springframework.stereotype.Service

@Service("echoService")
class EchoService {

    String echo(String value) {
        value?.reverse()
    }
}

The controller:

package test

import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

@RestController
class EchoController {

    final EchoService echoService

    EchoController(EchoService echoService) {
        this.echoService = echoService
    }

    @RequestMapping(
            value = "/echo/{message}",
            method = RequestMethod.GET,
            produces = MediaType.TEXT_PLAIN_VALUE
    )
    String doEcho(@PathVariable String message) {
        return echoService.echo(message)
    }
}

And the application class:

package test

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class Application {

    static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

And proof šŸ˜‰

āžœ curl localhost:8080/echo/hello
olleh%

Upvotes: 7

Related Questions