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

shouldBeInstanceOf

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

Verifies that this is instanceof T

Verifies that this value is an instance of T, which include any subclasses, and lets you execute block with that value casted.

Opposite of shouldNotBeInstanceOf

For an exact type, use shouldBeTypeOf

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

list.shouldBeInstanceOf<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.shouldBeInstanceOf<ArrayList<Int>>()

Parameters

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

Return
The typecasted instance