1

JUnit 5 has a neat extensions functionality which is not compatible with kotlintest even if it runs on JUnit framework. While the simple use cases in which we just need to log something can be handled by the TestListener, we cannot handle more advanced cases. In particular, how to interact with the extension? Ideally, I would like to get a hold of the extension so I could query it.

In JUnit5 it would be (one of the options anyway)

@ExtendWith(MyExtension.class)
class Something() {

 @MyAnnotation
 MyType myType;

 @Test
 void doSomething() {
    myType.doSomething();
 }

}

In JUnit4 it would be even simpler

@Rule
MyRule myRule;

@Test
void fun() {
  myRule.something();
}

Of course, there is a SpringExtension but it does the reflective instantiation of the class. Is there any way to do it easier?

kboom
  • 2,279
  • 3
  • 28
  • 43

1 Answers1

0

You can keep a reference to an extension/listener before passing it to the overriden function.

For example:

val myListener = MyKotlinTestListener()
val myOtherListener = MyOtherKotlinTestListener()


override fun listeners() = listOf(myListener, myOtherListener)

This way you can do what you want in your tests with that reference available

test("MyTest") {
    myListener.executeX()
}

The same goes if you're using an extension of any sort.

They'll still be executed as part of KotlinTest's lifecycle!

LeoColman
  • 6,950
  • 7
  • 34
  • 63