Skip to content

Builder<T>.isNotNull() fails with IllegalArgumentException inside and{ .... } #204

@MartinHaeusler

Description

@MartinHaeusler

Hi,

this one is similar to #203, but this time it's about the isNotNull() assertion. They might be related though.

Minimal example:

    @Test
    fun striktNullTest() {
        class Person(val name: String, val friend: Person?)
        // imagine we get this from an API which is nullable
        val p: Person? = Person("John", null)

        expectThat(p).isNotNull().and {
            get { name }.isEqualTo("John")
            get { friend }.isNotNull().and {  // this isNotNull() allows the following code to continue
                get { name }.isEqualTo("Jack")
            }
        }
    }

Expected: the usual nice strikt assertion failure message.

Actual:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method org.example.StriktTest$striktNullTest$1$3$1.invoke, parameter $receiver

	at org.example.StriktTest$striktNullTest$1$3$1.invoke(StriktTest.kt)
	at org.example.StriktTest$striktNullTest$1$3$1.invoke(StriktTest.kt:8)
	at strikt.internal.AssertionBuilder.get(AssertionBuilder.kt:92)
	at strikt.api.Assertion$Builder$DefaultImpls.get(Assertion.kt:233)
	at strikt.api.DescribeableBuilder$DefaultImpls.get(DescribeableBuilder.kt)
	at strikt.internal.AssertionBuilder.get(AssertionBuilder.kt:11)
	at org.example.StriktTest$striktNullTest$1$3.invoke(StriktTest.kt:20)
	at org.example.StriktTest$striktNullTest$1$3.invoke(StriktTest.kt:8)
	at strikt.internal.AssertionBuilder.and(AssertionBuilder.kt:37)
	at org.example.StriktTest$striktNullTest$1.invoke(StriktTest.kt:19)
	at org.example.StriktTest$striktNullTest$1.invoke(StriktTest.kt:8)
	at strikt.internal.AssertionBuilder.and(AssertionBuilder.kt:37)
	at org.example.StriktTest.striktNullTest(StriktTest.kt:17)

What I gather from this is that .isNotNull() allows the methods chained after it to continue execution when it's inside an and{ ... } block. Then, for the following methods, the receiver is (of course) null, because the .isNotNul() did not terminate the execution of the chain. The result is a runtime exception as shown above during the construction of the assertion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions