-
Notifications
You must be signed in to change notification settings - Fork 206
Closed
Description
Description
After upgrading from 6.1.0 to 6.2.0, eq() matcher throws NullPointerException when:
- The parameter type is a nullable value class (e.g.,
MyValueClass?) - The actual value is
null
This is a regression introduced in #545.
Reproduction
@JvmInline
value class MyValueClass(val value: Long)
interface Foo {
fun bar(x: MyValueClass?)
}
@Test
fun `eq with nullable value class and null value`() {
val foo = mock<Foo>()
foo.bar(null)
verify(foo).bar(eq(null)) // Throws NPE in 6.2.0+
}Error
java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "value" is null
at org.mockito.kotlin.MatchersKt.eqValueClass(Matchers.kt:91)
at org.mockito.kotlin.MatchersKt.eq(Matchers.kt:40)
Affected Versions
| Version | Status |
|---|---|
| 6.1.0 | ✅ Works |
| 6.2.0 | ❌ Broken |
| 6.2.1 | ❌ Broken |
Root Cause
In Matchers.kt, the eq() function checks T::class.isValue before checking if value is null:
inline fun <reified T : Any?> eq(value: T): T {
if (T::class.isValue) return eqValueClass(value) // Enters this branch even when value is null
// ...
}When T = MyValueClass? and value = null:
T::class.isValuereturnstrue(compile-time type is a value class)eqValueClass(null)is calledrequire(value::class.isValue)throws NPE becausenull::classis invalid
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels