kotest-assertions-core / io.kotest.matchers.types / kotlin.Any / shouldBeTypeOf

shouldBeTypeOf

inline fun <reified T : Any> Any?.shouldBeTypeOf(block: (T) -> Unit = { }): T

Verifies that this is exactly of type T

Verifies that this value is exactly of type T, where no inheritance is verified. If the assertion passes, you may use this as T inside block.

Opposite of shouldNotBeTypeOf

If you want to verify including inheritance, use shouldBeInstanceOf

val list: List<Int> = arrayListOf(1, 2, 3)

list.shouldBeTypeOf<ArrayList<Int>> { it: ArrayList<Int> // Typecasted for you if not explicit
// Use it
}
val list: List<Int> = arrayListOf(1, 2, 3)

// arrayList is typecasted to ArrayList<Int>()
val arrayList = list.shouldBeTypeOf<ArrayList<Int>>()

Parameters

block - Lambda that receives typecasted instance as argument for further assertions.

Return
The typecasted instance