I have a JUnit 5
test class written in Java
that uses a field annotated as @RegisterExtension
, and it works just fine:
class RegisterExtensionTest {
@RegisterExtension
LoggingExtension log = new LoggingExtension(RegisterExtensionTest.class);
@Test void demoTest() {
...
}
}
When I convert it to Kotlin:
class RegisterExtensionTest {
@RegisterExtension
var log = LoggingExtension(RegisterExtensionTest::class.java)
@Test
fun demoTest() {
...
}
}
Now the LoggingExtension
doesn't run any more. Why?
As documented here, just add a @JvmField
annotation to the field, and it works.
class RegisterExtensionTest {
@JvmField
@RegisterExtension
var log = LoggingExtension(RegisterExtensionTest::class.java)
@Test
fun demoTest() {
...
}
}
The extension field has to be public (and JUnit doesn't warn you if it isn't), but in Kotlin, a property creates a private field and a getter in the byte code, so JUnit doesn't see it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With