Kotest: compatibility with JUnit TestRule ?

Created on 12 Jul 2017  路  7Comments  路  Source: kotest/kotest

Is it possible to provide compatibility with JUnit TestRule ?
Several frameworks provide test utilities as TestRules, to be used with @Rule or @ClassRule (exemple : DropWizard's ResourceTestRule or DropwizardClientRule).
Unless there is a way to import or bridge with these TestRules, fully switching to kotlintest is not convenient.

Thanks for the nice work,

All 7 comments

Since TestRules are almost a thing of the past (discontinued with JUnit 5), I think it is not worth the effort. I'm not in the implementation details of JUnit rules but they look a bit hacky and the approach is somehow diametrical opposed to KotlinTest.

However the situation is not satisfying under certain conditions and I would be happy if someone would come up with an adapter that wouldn't affect the rest of KotlinTest.

thanks for the answser.
Not sure myself how to build such an adapter, emulating the necessary org.junit.runners.model.Statement and org.junit.runner.Description seems a bit complicated.

What's the recommended way to do something like the TemporaryFolder rule in kotlintest then? Do we need to manually implement this via interceptTestCase?

I have a legitimate use case for supporting @Rule. The Android framework and its helper libraries rely on it for test behavior.

One example is the just released architecture-components: https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/test/java/com/android/example/github/ui/user/UserViewModelTest.java#L55

Without that rule it's impossible to unitest that library, as it tries to find a Thread that doesn't exist outside Android environments, and fails with NPE.

JUnit rules are on their way out with the appearance of JUnit 5, I think. But maybe we could find another solution.

Maybe you want to have a look at KotlinTest interceptors.

Was this page helpful?
0 / 5 - 0 ratings