koufa
11/14/2025, 1:10 PMAlfonso Ristorato
11/14/2025, 5:16 PMobject DatabaseCleanerAfterSpecListener : AfterSpecListener {
override suspend fun afterSpec(spec: Spec) {
runCatching {
val manager = TestContextManager(spec::class.java)
val context = manager.testContext.applicationContext
val jdbcTemplate = context.getBean(JdbcTemplate::class.java)
GenericTestUtils.DataCleanerUtils.cleanData(
jdbcTemplate,
GenericTestUtils.AllTables.tablesList,
)
}
}
}
hopefully it inspires a solution for you 🙂koufa
11/15/2025, 7:33 PMBeforeSpecListener as the Spring Context is available there. What I want to achieve though is to have a callback that runs only once for all Specs and this is a Listener like BeforeProject . Unfortunately the Spring Context is not available in that caseAlfonso Ristorato
11/15/2025, 10:20 PMbeforeProject, my bad. I guess that would be quite difficult, as Spring would not have been initialised by then.
How about this, while we think on how/if to introduce it in kotest?
object OnlyOnceBeforeSpecListener: BeforeSpecListener{
var hasRun = false
override suspend fun beforeSpec(spec: Spec) {
if(hasRun) return
runCatching {
val manager = TestContextManager(spec::class.java)
val context = manager.testContext.applicationContext
val bean = context.getBean(WhateverYourBean::class.java)
}.onFailure {
println("this spec does not have spring context, not going to do anything")
}.onSuccess {
println("done with before spec setup")
hasRun = true
}
}
}