Test not found when run from gradle
See original GitHub issueUsing Gradle 4.6 and KotlinTest 3.0.1, executing gradlew clean test
returns a bunch of warnings from org.reflections, but does not run any tests. When run using the junit runner within intellij, it works fine.
I’m currently trying to run a spring-boot integration test, shown below. I’ve also listed my build.gradle for reference.
test class:
@SpringBootTest
class SampleRepositoryIntegrationSpec : BehaviorSpec() {
override fun listeners(): List<TestListener> = listOf(SpringListener)
@Autowired
lateinit var repository: SampleRepository
init {
given("A valid instance of Sample") {
val expected = Sample(comment = UUID.randomUUID().toString())
When("it is persisted") {
val persisted = repository.save(expected)
then("a valid uuid has been generated") {
persisted.uuid shouldNotBe null
}
When("it is queried by uuid") {
val found = repository.findOne(persisted.uuid)
then("is found") {
found shouldNotBe null
found.uuid shouldBe persisted.uuid
}
}
}
}
}
}
Build.gradle:
buildscript {
ext {
spring_boot_version = ‘1.5.10.RELEASE’
spring_version = ‘4.3.14.RELEASE’
kotlin_version = ‘1.2.31’
junit_version = ‘5.1.0’
junit_plugin_version = ‘1.1.0’
kotlin_test_version = ‘3.0.1’
}
repositories {
mavenCentral()
}
dependencies {
classpath “org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version”
classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”
classpath “org.jetbrains.kotlin:kotlin-allopen:$kotlin_version”
classpath “org.jetbrains.kotlin:kotlin-noarg:$kotlin_version”
classpath “org.junit.platform:junit-platform-gradle-plugin:$junit_plugin_version”
}
}
apply plugin: ‘org.springframework.boot’ apply plugin: ‘war’ apply plugin: ‘java’ apply plugin: ‘kotlin’ apply plugin: ‘kotlin-spring’ apply plugin: ‘kotlin-jpa’ apply plugin: ‘org.junit.platform.gradle.plugin’
sourceCompatibility = 1.8
repositories { mavenCentral() }
configurations { providedRuntime }
springBoot { buildInfo() }
dependencies { //Kotlin compile “org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version” compile “org.jetbrains.kotlin:kotlin-reflect:$kotlin_version”
//Load core spring-boot starters:
compile "org.springframework.boot:spring-boot-starter:$spring_boot_version"
compile "org.springframework:spring-web:$spring_version"
//Load data-access dependencies:
compile "org.springframework.boot:spring-boot-starter-data-jpa:$spring_boot_version"
runtime 'mysql:mysql-connector-java'
providedCompile "org.springframework.boot:spring-boot-starter-tomcat:$spring_boot_version"
//Load test dependencies
testCompile 'org.springframework:spring-context'
testCompile 'org.springframework:spring-test'
testCompile "org.springframework.boot:spring-boot-test:$spring_boot_version"
testCompile "org.junit.platform:junit-platform-launcher:$junit_plugin_version"
testCompile "org.junit.platform:junit-platform-runner:$junit_plugin_version"
testCompile "io.kotlintest:kotlintest-runner-junit5:$kotlin_test_version"
testCompile "io.kotlintest:kotlintest-extensions-spring:$kotlin_test_version"
}
compileKotlin { kotlinOptions.jvmTarget = java_version } compileTestKotlin { kotlinOptions.jvmTarget = java_version }
bootRun { systemProperties[“spring.profiles.active”] = System.properties[“spring.profiles.active”] ?: “development” }
test { useJUnitPlatform() systemProperties[“spring.profiles.active”] = System.properties[“spring.profiles.active”] ?: “test” testLogging { exceptionFormat = ‘full’ } }
Issue Analytics
- State:
- Created 5 years ago
- Comments:33 (33 by maintainers)
Top GitHub Comments
I found why the ‘active-profile’ wasn’t working. It’s nothing related to Kotlin-Test; when using Junit5 and spring-boot, JUnit5 w/ Gradle 4.6+ recommends using
useJUnitPlatform()
within the “test” task, rather than the (now obsolete) gradle-plugin. Doing this means thatsystemProperties["spring.profiles.active"]
must be set within JUnit’s “junitPlatformTest” task, rather than the standard “test” task. That was the root issue.Aha, well thanks for coming back and updating the ticket.