如何使用 gradle 6+ 和 java 11+ 在 Spring Boot 中配置 spock

Posted

技术标签:

【中文标题】如何使用 gradle 6+ 和 java 11+ 在 Spring Boot 中配置 spock【英文标题】:How to configure spock in spring boot with gradle 6+ and java 11+ 【发布时间】:2020-11-25 15:51:59 【问题描述】:

我想在我的 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 boot?

【问题讨论】:

gradle 7? 6.5.1 是最新的...gradle.org/releases 确实是错字,已编辑! 【参考方案1】:

认为你陷入了依赖依赖的 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)
    

以及应用类:

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%

【讨论】:

你是对的,gradle config 现在可以了。非常感谢!

以上是关于如何使用 gradle 6+ 和 java 11+ 在 Spring Boot 中配置 spock的主要内容,如果未能解决你的问题,请参考以下文章

摇篮 4 Java 11

使用 google play-services-maps 11.6.2 的 Android Studio Gradle 同步错误

如何使用 Gradle 6.1.1 为 APK 设置压缩?

即使 java gradle 工具链设置为 jdk 11,Gradle 也使用 Java 1.8 构建 android 项目

Java 11、JavaFX、Gradle、Eclipse,令人讨厌的失败消息

无法从 Java 源使用最新的 gradle 构建工具 (3.6.3) 生成 APK,错误引用 Kotlin