首页 > 解决方案 > 无法在 Spring 中为集成测试配置模块 - 没有可用的任务

问题描述

我在为Spring使用Groovy和的集成测试配置模块时遇到了麻烦Spock。在我单击run按钮(在 Intellij 中)后,它说

没有可用的任务

到目前为止我所做的是:

package gcptraindata


import gcptraindata.mysql.TestDataSourceConfig
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.web.WebAppConfiguration
import spock.lang.Specification

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = [TestDataSourceConfig])
@WebAppConfiguration
class IntegrationSpec extends Specification {

@Autowired
protected TestRestTemplate restTemplate

def 'sample test'(){
    given:
        def bla = 0

    when:
        bla += 2

    then:
        bla == 2
}


}

和数据源:

package gcptraindata.mysql

import org.springframework.boot.autoconfigure.flyway.FlywayDataSource
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary

import javax.sql.DataSource

@Configuration
class TestDataSourceConfig {

@Primary
@Bean
@FlywayDataSource
DataSource dataSource() {
    return DataSourceBuilder.create()
            .driverClassName('com.mysql.jdbc.Driver')
            .username('gcpuser')
            .password('Domin')
            .url('jdbc:mysql://${MYSQL_HOST:localhost}:3306/test?serverTimezone=UTC')
            .build() as DataSource

}

}

我想在我的 build.gradle 中配置所有内容,所以这里是:

    plugins {
    id 'java'
    id 'groovy'
    id 'application'
    id 'org.springframework.boot' version '2.1.7.RELEASE'
}

    apply plugin: 'java'
    apply plugin: 'groovy'
    apply plugin: 'io.spring.dependency-management'


    project.group = 'gcp-train-data'
    project.version = '0.0.1'
    mainClassName = 'gcptraindata.AppRunner'

repositories {
    mavenCentral()
    jcenter()
}

configurations {
    developmentOnly
    integrationCompile.extendsFrom testCompile
}

sourceSets {
    integration {
        java.srcDir file('src/integration/groovy')
    }
}

jar {
    enabled = true
}

dependencies {
    compile group:      'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group:      'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group:      'org.springframework.boot', name: 'spring-boot-starter-jdbc'
    compile group:      'org.springframework.boot', name: 'spring-boot-configuration-processor'
    compile group:      'org.codehaus.groovy',      name: 'groovy-all',             version: '2.5.7'
    compile group:      'mysql',                    name: 'mysql-connector-java',   version: '5.1.47'

    annotationProcessor    group: 'org.projectlombok',         name: 'lombok'

    integrationCompile group: 'org.spockframework',        name: 'spock-core',               version: '1.3-groovy-2.5'
    integrationCompile group: 'org.spockframework',        name: 'spock-spring',             version: '1.3-groovy-2.5'
    integrationCompile group: 'org.springframework.boot',  name: 'spring-boot-starter-test'
    integrationCompile group: 'org.codehaus.groovy',       name: 'groovy-all',               version: '2.5.7'
}

我在这里错过了什么?

标签: javaspringgradlegroovyspock

解决方案


我不认为sourceSets设置实际运行Spock测试所需的所有任务。我认为缺少一个测试任务来运行编译的测试。

我建议为此使用另一个Gradle插件:gradle-testsets-plugin

更改后的build.gradle现在有一个名为的任务integration,您可以使用它通过 Gradle: 运行测试gradlew integration。我还能够在IntelliJ中运行测试。

plugins {
    id 'java'
    id 'groovy'
    id 'application'
    id 'org.springframework.boot' version '2.1.7.RELEASE'

    // add plugin
    id 'org.unbroken-dome.test-sets' version '2.2.1'
}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'io.spring.dependency-management'


project.group = 'gcp-train-data'
project.version = '0.0.1'
mainClassName = 'gcptraindata.AppRunner'

repositories {
    mavenCentral()
    jcenter()
}

// define custom testSet
// this replaces the customm sourceSets configuration
testSets {
    integration
}

configurations {
    developmentOnly
    integration.extendsFrom testCompile
}

jar {
    enabled = true
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'
    compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.5.7'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'

    annotationProcessor group: 'org.projectlombok', name: 'lombok'

    integrationCompile group: 'org.spockframework', name: 'spock-core', version: '1.3-groovy-2.5'
    integrationCompile group: 'org.spockframework', name: 'spock-spring', version: '1.3-groovy-2.5'
    integrationCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    integrationCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.5.7'
}

注意任务列表中的额外任务

$ ./gradlew tasks

...

Verification tasks
------------------
check - Runs all checks.
integration - Runs the integration tests.
test - Runs the unitTest tests.

推荐阅读