Can I use JUnit5 Extensions with KoTest? If yes, h...
# kotest
h
Can I use JUnit5 Extensions with KoTest? If yes, how? Just adding the annotation for an Extension does not seem to execute it for me.
s
No, we have a wrapper but it's not very well tested.
What rule is it you want to use, there might be a port of it
h
Thanks for the quick reply. Is there another way to reuse code which is used in multiple test classes? The extension I am using is described in this blog post:
s
Kotest is just Kotlin so you have way more options to sharing code.
If you want to reuse code, pull it into a function ?
h
Extensions have the benefit that you can group setup and teardown code together and than apply them together. When I offer common code I would have to make sure to use the correct setup and teardown method manually in my tests. But maybe it is possible to create a pattern where you call external
berfore {} / after {}
blocks. I will tinker a bit 🙂
s
Got it. So what you might wanna do in Kotest is create a TestListener.
Copy code
class/object Foo : TestListener {
  override fun beforeTest ... // set up
  override fun afterTest ... // tear down
}
Then you can add that to whatever tests you want.
Copy code
class MyTest : FunSpec({
   listener(Foo())
   test("....) { }
})
Not the best docs but more info here: https://kotest.io/docs/framework/listeners.html
h
Yes, perfect! Thanks 🙂
s
Cool, feel free to hit me up if you have anymore questions
🙇 1
l
specifically for LiveData, I wrote this listener and use it on a daily basis. Not sure why I didn't ship it to Kotest yet, but it works for my usecase
h
Haha that looks exactyl like the code I ended up with. I did add super calls because the IDE generated them for me. Not sure if needed though.
👍 1
👍🏻 1