我在使用 Groovy
和 Spock
配置用于 Spring
集成测试的模块时遇到麻烦。在我点击 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 集成
。我还能够在 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/