Having a Unit test class with some arguments defined in construction
class VersionConsumerSpec {
private val path = Paths.get("target").toAbsolutePath().toString()
private val kafkaPort = (4000..8000).random()
private val zooKeeperPort = (4000..8000).random()
private val config: AppConfig = createAppConfig(kafkaPort)
private val producer = createKafkaProducer()
init {
println("hello world")
}
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
@Test
@DisplayName("test1")
fun test1() {
}
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
@Test
@DisplayName("test2")
fun test2() {
}
}
All the variables defined are executed for every test, even the init
is executed twice here.
Then I'm having issues since I want to declare all those variables once for all test cases.
I´m going something wrong here?, in Java or Scala this is not happening.
Is like if for each test the class would be completely instantiate again and again.
I guess that you are looking for @TestInstance annotation. PER_CLASS
mode allows to create a new test instance once per test class.
@TestInstance(PER_CLASS)
class VersionConsumerSpec {
...