java - 无法在 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'
}
我在这里错过了什么?
解决方案
我不认为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.
推荐阅读
- c++ - 在指针数组中进行投射和写入会使用 clang sanitizer 报告未对齐的地址
- r - 从 R 中的 PLS 中提取系数/变量以用于决策树或其他类型的模型
- python - 如何预测 keras 中的下一个值?
- oracle - Oracle 如何将数据类型映射到 CODE/ID - Dump 和 dbms_sql.define_column 生成数据类型代码
- javascript - 在 Google App Script 中将一张工作表中的原始数据排序到新工作表上的单独选项卡中
- amazon-web-services - 如果 docker install with amazon-linux-extras 不成功,则停止 Ansible playbook
- indy - 上传到跳过 Web 服务器
- c# - LimitedConcurrencyLevelTaskScheduler 和 aync/await 的锁定问题
- azure - 如何阻止出站 HTTP 连接超时
- cakephp - CakePhp3.7 插件自动加载选项不可用