You can define an extension on Spec
that sets up the desired fixture and then apply it in your Spek
s as follows:
fun Spec.setUpFixture() {
beforeEachTest { println("before") }
afterEachTest { println("after") }
}
@RunWith(JUnitPlatform::class)
class MySpek : Spek({
setUpFixture()
it("should xxx") { println("xxx") }
})
Though this is not exactly what you asked, it still allows flexible code reuse.
UPD: This is a working option with Spek
s inheritance:
open class BaseSpek(spec: Spec.() -> Unit) : Spek({
beforeEachTest { println("before") }
afterEachTest { println("after") }
spec()
})
@RunWith(JUnitPlatform::class)
class MySpek : BaseSpek({
it("should xxx") { println("xxx") }
})
Basically, do do this, you invert the inheritance direction, so that the child MySpek
passes its setup in the form of Spec.() -> Unit
to the parent BaseSpek
, which adds the setup to what it passes to Spek
.