Update: From version 4.3+, somethings have changes.
Version 4.3+ (newer)
KotlinTest was renamed to Kotest. The SpringExtension
is the class to integrate with spring. The documentation on how to use it is pretty simple.
In addition to Kotest
dependency, you should also add SpringExtension
:
testImplementation("io.kotest.extensions:kotest-extensions-spring:1.0.0")
and then include SpringExtension
to your extensions. From then on you can use everything Spring Boot
has to offer in tests, such as ContextConfiguration
and others:
@SpringBootTest
class MyTestSpec : FunSpec() {
override fun extensions() = listOf(SpringExtension)
init {
test("I'm inside a Spring context!") { }
}
}
Version 3.3.2 (very old)
Kotlintest has a very nice guide on how to setup Spring Extension to test Spring Framework.
Basically, in addition to KotlinTest
dependency, you also add it's Spring Extension
:
testImplementation ("io.kotlintest:kotlintest-runner-junit5:3.3.2") // KT dependency
testImplementation("io.kotlintest:kotlintest-extensions-spring:3.3.2") // KT Spring Extensions
And then you include the SpringListener
and SpringBootTest
to your code:
import org.springframework.boot.test.context.SpringBootTest
import io.kotlintest.spring.SpringListener
@SpringBootTest
class SpringExample : FreeSpec() {
override fun listeners() = listOf(SpringListener)
init {
"Verify context loads" {
}
}
}
You don't need to add SpringListener
to every test you create, you can configure it globally using ProjectConfig
. ProjectConfig is explained in KotlinTest Documentation