如何使用 gradle 6+ 和 java 11+ 在 spring 引导中配置 spock

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

我想在我的 spring-boot 项目中使用 spock(使用 groovy,而不是 java)。我有一些 spock 项目已经在 java 8 和更低版本的 gradle 中运行,但我找不到有关最新版本的文档。

这是我的旧 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',
    )
}

这行得通,我可以创建 spock 测试,但是当我使用 java 11 和 gradle 6.4 创建项目时,一切都不再有效(gradle 语法有很大不同,并且spock 库不再工作),

这是我的当前(失败)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()
}

有了这个,除了 gradle 构建没有找到 spock-spring 库外,没有任何效果。

如何使用最新版本的 java 和 gradle 将 spock 库应用到 spring 引导?

认为您陷入了 Groovy 具有依赖项的版本的混乱:

我用这个版本测试过:

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()
}

并且这个测试 运行 并通过了:

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")
    }
}

(显然,我的上下文中有一个 echoService

为了完整起见,这里是服务:

package test

import org.springframework.stereotype.Service

@Service("echoService")
class EchoService {

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

控制器:

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)
    }
}

和应用程序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);
    }
}

并证明

➜ curl localhost:8080/echo/hello
olleh%