首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用gradle 6+和java 11+在spring boot中配置spock

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

Stack Overflow用户
提问于 2020-08-05 20:55:26
回答 1查看 3.7K关注 0票数 3

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

这是我以前的build.gradle

代码语言:javascript
复制
    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:

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-05 21:52:48

我想你已经混杂了Groovy版本的依赖关系了:

我使用这个构建进行了测试:

代码语言:javascript
复制
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()
}

这个测试运行并通过了:

代码语言:javascript
复制
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 )

为了完整起见,下面是服务:

代码语言:javascript
复制
package test

import org.springframework.stereotype.Service

@Service("echoService")
class EchoService {

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

控制器:

代码语言:javascript
复制
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)
    }
}

和应用程序类:

代码语言:javascript
复制
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);
    }
}

和证明?

代码语言:javascript
复制
➜ curl localhost:8080/echo/hello
olleh%
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63265708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档