kotest-assertions-core / io.kotest.matchers.nulls / kotlin.Any / shouldNotBeNull

shouldNotBeNull

fun Any?.shouldNotBeNull(): Unit

Verifies that this is not null

Matcher to verify that a specific nullable reference is not null. Opposite of shouldBeNull

Example:

    val nullable: String? = null
    val nonNull: String? = "NonNull"

    nonNull.shouldNotBeNull()     // Passes
    nullable.shouldNotBeNull()    // Fails

Note: This function uses Kotlin Contracts to tell the compiler that this is not null. So after this is used, all subsequent lines can assume the value is not null without having to cast it. For example:

    val nonNull: String? = "NonNull"

    nonNull.shouldNotBeNull()
    useNonNullString(nonNull)


    // Notice how this is a not-nullable reference
    fun useNonNullString(string: String) { }