I have a kotlin kotest (formerly known as kotlintest) BehaviorSpec
with one Given("...")
and many When("...") Then("...")
under it
I want to execute a cleanup after the whole Spec (respectively every Given
clause) has finished.
@MicronautTest
class StructurePersistSpec(
private val iC : InstancesC
) : BehaviorSpec({
// afterSpec {
finalizeSpec {
cleanup()
}
Given("...") {
When("...") {
Then("...") {
...
}
Then("...") {
...
}
}
When("...") {
Then("...") {
...
}
Then("...") {
...
}
}
}
...
}
on using afterSpec { }
I get multiple calls (amount of When
s??) to the afterSpec { }
clause and NOT just one after the Spec finished (or finishing of the/each Given
Clause)
on using finalizeSpec { }
it does NOT get called at all (breakpoint inside it is never hit)
what am I doing wrong?
or did I miss some fancy characteristics of BehaviorSpec
s ?
The reason you are getting multiple calls is that probably you have set a different
IsolationMode
for your test.That would mean your Spec will be recreated (and then cleaned) for every test. In order to have a single
afterSpec
call from the framework, yourIsolationMode
must be set toSingleInstance
.Bare in mind that might affect the way your tests are being executed hence their validity or ability to pass.
Documentation: https://kotest.io/isolation_mode/