doc / ch.tutteli.atrium.api.fluent.en_GB / notToBeNull

notToBeNull

inline fun <reified T : Any> Expect<T?>.notToBeNull(): Expect<T> (source)

Expects that the subject of this expectation is not null and changes the subject to the non-nullable version.

expect<Int?>(1)
    .notToBeNull() // subject is now of type Int
    .isLessThan(2)

fails {
    expect<Int?>(null)
        .notToBeNull() // fails
        .isLessThan(2) // not shown in reporting as notToBeNull already fails
}

Return
An Expect with the non-nullable type T (was T? before).

inline fun <reified T : Any> Expect<T?>.notToBeNull(noinline assertionCreator: Expect<T>.() -> Unit): Expect<T> (source)

Expects that the subject of this expectation is not null and that it holds all assertions the given assertionCreator creates.

expect<Int?>(1).notToBeNull { // subject is now of type Int, within this block but also afterwards
    isGreaterThan(0)
    isLessThan(10)
}.toBe(1)

fails {
    // because you forgot to define an assertion in the assertion group block
    // use `notToBeNull()` if this is all you want to assert
    expect<Int?>(1).notToBeNull { }
}


fails {
    // notToBeNull already fails, reporting mentions that subject was expected `to be: 2`
    expect<Int?>(null).notToBeNull {
        toBe(2)
    }
}

fails {
    // sub-assertion fails
    expect<Int?>(1).notToBeNull {
        isLessThan(0)
    }
}

Return
An Expect with the non-nullable type T (was T? before)