What is proper workaround for @BeforeAll in Kotlin
You have access to the variables inside the companion object:
companion object {
private lateinit var objectToBeInitialized: Test
@BeforeAll
@JvmStatic
fun setup() {
objectToBeInitialized = Test()
}
}
JUnit 5 has @TestInstance(PER_CLASS)
annotation that can be used for this purpose. One of the features that it enables is non-static BeforeAll
and AfterAll
methods:
@TestInstance(PER_CLASS)
class BeforeAllTests {
lateinit var isInit = false
@BeforeAll
fun setup() {
isInit = true
}
@TestFactory
fun beforeAll() = listOf(
should("initialize isInit in BeforeAll") {
assertTrue(isInit)
}
)
}
fun should(name: String, test: () -> Unit) = DynamicTest.dynamicTest("should $name", test)