I'm facing troubles in configuring module for integration tests for Spring
using Groovy
and Spock
. After I click run
button (in Intellij) it says
no tasks available
What I've done so far is:
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
}
}
and datasource:
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
}
}
I want to have configured everything in my build.gradle, so here it is:
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'
}
What did I miss here?
I don't think sourceSets
set up all tasks you need to actually run your Spock tests. I think there is a test task missing to run the compiled tests.
I would recommend using another Gradle plugin for that: gradle-testsets-plugin
The changed build.gradle
now has a task named integration
that you can use to run the tests via Gradle: gradlew integration
. I was also able to run the test within 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'
}
Note the extra task in the task list
$ ./gradlew tasks
...
Verification tasks
------------------
check - Runs all checks.
integration - Runs the integration tests.
test - Runs the unitTest tests.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.