Skip to content

Commit 2431eb6

Browse files
authored
Fix eq() matcher NPE with nullable value class and null value (#579)
1 parent 13bec57 commit 2431eb6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import org.mockito.kotlin.internal.valueClassInnerClass
3737

3838
/** Matches an argument that is equal to the given value. */
3939
inline fun <reified T : Any?> eq(value: T): T {
40-
if (T::class.isValue) return eqValueClass(value)
40+
if (value != null && T::class.isValue) return eqValueClass(value)
4141

4242
return ArgumentMatchers.eq(value) ?: value
4343
}
@@ -87,10 +87,10 @@ inline fun <reified T> anyValueClass(): T {
8787
}
8888

8989
/** Matches an argument that is equal to the given Kotlin value class value. */
90-
inline fun <reified T> eqValueClass(value: T): T {
90+
inline fun <reified T : Any> eqValueClass(value: T): T {
9191
require(value::class.isValue) { "${value::class.qualifiedName} is not a value class." }
9292

93-
val unboxed = value?.unboxValueClass()
93+
val unboxed = value.unboxValueClass()
9494
val matcher = AdditionalMatchers.or(ArgumentMatchers.eq(value), ArgumentMatchers.eq(unboxed))
9595

9696
return (matcher ?: unboxed).toKotlinType(T::class)

tests/src/test/kotlin/test/MatchersTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,24 @@ class MatchersTest : TestBase() {
672672
verify(this).nestedValueClass(eq(nestedValueClass))
673673
}
674674
}
675+
676+
@Test
677+
fun eqNullableValueClass_nullValue() {
678+
val valueClass = null as ValueClass?
679+
mock<SynchronousFunctions>().apply {
680+
nullableValueClass(valueClass)
681+
verify(this).nullableValueClass(eq(valueClass))
682+
}
683+
}
684+
685+
@Test
686+
fun eqNullableLongValueClass_nullValue() {
687+
val longValueClass = null as LongValueClass?
688+
mock<SynchronousFunctions>().apply {
689+
nullableLongValueClass(longValueClass)
690+
verify(this).nullableLongValueClass(eq(longValueClass))
691+
}
692+
}
675693
}
676694

677695
class OtherMatchersTest {

0 commit comments

Comments
 (0)