The integration test of Spring boot application always starts the web server firstly.
The simplest test of spring boot test looks like below, how does migrate it using kotlintest instead?
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ReportApplicationTests {
@Test
fun `Server can be launched`() {
}
}
Here's how I've got it set up: firstly, make sure to reference JUnit 5 instead of 4, e.g. I've got this in the dependencies
section of my build.gradle
:
testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit"
testImplementation "io.kotlintest:kotlintest-extensions-spring:3.1.10"
testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.1.10'
testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.1"
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.3.1"
Also add this to build.gradle
:
test {
useJUnitPlatform()
}
Then in your integration test class have this (notice the override of listeners
, without which it won't work):
import org.springframework.boot.test.context.SpringBootTest
import io.kotlintest.spring.SpringListener
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [MyApplication::class])
class MyTestStringSpec : StringSpec() {
override fun listeners() = listOf(SpringListener)
init {
// Tests go in here
}
}
Obviously you can replace StringSpec
with any of the other Kotlin Test testing styles, e.g. FunSpec
, ShouldSpec
, etc.