Davio
12/11/2025, 2:12 PMthing.shouldBeNull()
And thing is not null, you get a weird error in IntelliJ:
org.opentest4j.AssertionFailedError: Expected value to be null, but was thing.
Expected :null
Actual :null
Actual here should be able to reuse thing instead of just null.
I traced this back to InvokeMatcher.kt where for this case a builder without expected / actual value is created:
AssertionErrorBuilder.create().withMessage(result.failureMessage()).build()
And it gets there because the Matcher for beNull is a simple matcher that it doesn't support
fun beNull() = object : Matcher<Any?> {
override fun test(value: Any?): MatcherResult {
val passed = value == null
return MatcherResult.invoke(
passed,
{ "Expected value to be null, but was $value." },
{ "Expected value to not be null, but was null." }
)
}
}
If I replace this implementation with:
object : Matcher<Any?> {
override fun test(value: Any?): ComparisonMatcherResult {
val passed = value == null
return ComparisonMatcherResult(
passed,
value.print(),
null.print(),
{ "Expected value to be null, but was $value." },
{ "Expected value to not be null, but was null." },
)
}
}
I get a nicer message where the actual is properly filled with the real valueDavio
12/11/2025, 2:20 PMAlex Kuznetsov
12/11/2025, 9:02 PMAlex Kuznetsov
12/11/2025, 9:03 PMAlex Kuznetsov
12/11/2025, 9:30 PMimport io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.nulls.shouldBeNull
class ShouldBeNullTest: StringSpec() {
init {
"should fail with expected message" {
val notNullThing = "NotNullThing"
notNullThing.shouldBeNull()
}
}
}
Expected value to be null, but was NotNullThing.Davio
12/12/2025, 8:30 AMDavio
12/12/2025, 8:31 AMorg.opentest4j.AssertionFailedError: Expected value to be null, but was thing.
Expected :null
Actual :null
This is the console output in IntelliJDavio
12/12/2025, 8:31 AMAlex Kuznetsov
12/12/2025, 1:45 PMAlex Kuznetsov
12/12/2025, 2:32 PMDavio
12/12/2025, 3:02 PMDavio
12/12/2025, 3:04 PMimport io.kotest.matchers.nulls.shouldBeNull
import org.junit.jupiter.api.Test
class ShouldBeNullTest {
@Test
fun `should fail with expected message`() {
val notNullThing = "NotNullThing"
notNullThing.shouldBeNull()
}
}
Then the output is:
org.opentest4j.AssertionFailedError: Expected value to be null, but was NotNullThing.
Expected :null
Actual :null
<Click to see difference>
You have a clickable link with "click to see difference" and that is now very weird, because if you click on it , you just get a diff of 2 null valuesDavio
12/15/2025, 8:19 AM