gpt4 book ai didi

java - 无法在 Spring 中为集成测试配置模块 - 没有可用的任务

转载 作者:行者123 更新时间:2023-12-03 03:11:06 25 4
gpt4 key购买 nike

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

no tasks available



到目前为止我所做的是:
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'
}

我在这里错过了什么?

最佳答案

我不认为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.

关于java - 无法在 Spring 中为集成测试配置模块 - 没有可用的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58205098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com