Skip to content

Conversation

@KonaEspresso94
Copy link

Thank you for submitting a pull request! But first:

  • Can you back your code up with tests?
  • Please run ./gradlew spotlessApply :tests:spotlessApply for auto-formatting.

Summary

This PR fixes the NPE issue in eq() matcher when used with nullable value class and null value.

Fixes #577

Problem

After #545 added value class support to eq(), using eq(null) with a nullable value class type throws NullPointerException:

@JvmInline
value class MyValueClass(val value: Long)

interface Foo {
    fun bar(x: MyValueClass?)
}

// This throws NPE in 6.2.0+
verify(foo).bar(eq(null))

Root Cause

In eqValueClass(), the code accesses value::class before checking if value is null:

inline fun <reified T> eqValueClass(value: T): T {
    require(value::class.isValue) { ... }  // NPE when value is null
    ...
}

Solution

Add a null check at the beginning of eqValueClass():

if (value == null) {
    @Suppress("UNCHECKED_CAST")
    return ArgumentMatchers.eq<T>(null) as T
}

This follows the existing pattern used in toKotlinType() in ValueClassSupport.kt.

Changes

  • Matchers.kt: Add null check in eqValueClass()
  • MatchersTest.kt: Add test cases for nullable value class with null value

@TimvdLippe
Copy link
Contributor

@m-koops FYI the gift that keeps on giving

@m-koops
Copy link
Contributor

m-koops commented Jan 19, 2026

@KonaEspresso94 Thank you very much for raising the NPE issue and providing your fix in this PR. While reviewing the change, I had to make some slight adjustments.

Therefore I decided to prepare a new commit/PR (#579). @TimvdLippe can you review that new PR?

@KonaEspresso94
Copy link
Author

@TimvdLippe @m-koops Thanks for your quick feedback. I’ll close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants