https://kotlinlang.org logo
Title
w

wasyl

08/01/2022, 11:26 AM
Hi there, quick question about Listeners: I see that
Listener
class is deprecated, and
listeners()
method on
AbstractProjectConfigs
suggests to use `Extension`s instead. However,
TestListener
interface (and all the
BeforeEachListener
and friends) is not deprecated. Is it an oversight, or I should keep implementing a
TestListener
and registering it as an extension?
If I should use an extension, do I understand correctly that
class MyListener : TestListener {
  override suspend fun prepareSpec() = foo()
  override suspend fun finalizeSpec() = bar()
}
should be translated to
class MyExtension : SpecExtension {
  override suspend fun intercept(spec, execute) {
    foo()
    execute(spec)
    bar()
  }
}
s

sam

08/01/2022, 11:38 AM
Keep using TestListener and just register them in the extensions method
Listeners are extensions too
w

wasyl

08/01/2022, 11:43 AM
Thanks 🙂 Btw would the extension I wrote above be equivalent to the test listener, or I would break something?
s

sam

08/01/2022, 11:45 AM
Same
w

wasyl

08/01/2022, 11:47 AM
Many thanks, btw I finally updated to 5.x and it works perfect now 🙂 Thanks for all the effort you put into the library 🙏
s

sam

08/01/2022, 12:00 PM
Glad you're enjoying it :)
w

wasyl

08/01/2022, 12:12 PM
fyi actually the above migration broke something 😄 I think perhaps prepare/finalize is called before the spec is instantiated, and SpecExtension is called when it’s already created. No big deal, just wanted to put it out there in case it’s useful for someone in the future
s

sam

08/01/2022, 12:14 PM
There's two spec extensions. Try the other one sorry.
w

wasyl

08/01/2022, 12:20 PM
The other one would be
SpecRefExtension
?
s

sam

08/01/2022, 12:52 PM
I think so, can't remember off the top of my head. One is called before the spec is created (what you want) and one is called after (equiv to BeforeSpecListener / AfterSpecListener)
Unless you specifically want the around advice capability, you can stick with the listeners. They are not deprecated. The word listener is just used here to mean it's side effecting only.
w

wasyl

08/01/2022, 12:58 PM
Yep, I was just curious — ended up keeping a listener 🙂