Skip to content

eq() matcher throws NPE when used with nullable value class and null value #577

@KonaEspresso94

Description

@KonaEspresso94

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:

  1. T::class.isValue returns true (compile-time type is a value class)
  2. eqValueClass(null) is called
  3. require(value::class.isValue) throws NPE because null::class is invalid

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions