Why does unit testcase run before the init block in companion object?

40 Views Asked by At

I have three enum class with init block in companion object. All of them have the same method like XXX.register()

@Test
fun aTest(){
    val objectMapper = ObjectMapper()
    val readValue: InterfaceA = objectMapper.readValue(jsonString, InterfaceA::class.java)
    MatcherAssert.assertThat(readValue.XXXType,Is.`is`(InterfaceA.EnumA.SomeInstance))
}
    
enum class A: InterfaceA{
companion object {
    init {
        InterfaceA.register(enumA.values())
    }
}

InterfaceA{
  companion object {

    private val subClassRegisterCache :MutableMap<String, InterfaceA> = mutableMapOf()
    fun <T:InterfaceA> register(array: Array<T>){
        array.map { subClassRegisterCache.put(it.getName(),it)
    }

  }
}

But when I run a test case in debug mode, it shows running order like

  1. enumB.init
  2. testcase
  3. enumA.init
  4. enumC.init

In my mind, the order should be like

  1. any enum.init
  2. any rest enum.init
  3. last enum.init
  4. testcase

Any idea?

0

There are 0 best solutions below