From c8b840a806ebdc507927b166245b70916d2683aa Mon Sep 17 00:00:00 2001 From: hunter-werlla Date: Thu, 6 Jul 2023 14:42:56 -0700 Subject: [PATCH] Fix issue #480 by widening the allowed types for lenient().whenever() --- .../kotlin/org/mockito/kotlin/LenientStubber.kt | 4 ++-- tests/src/test/kotlin/test/LenientStubberTest.kt | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/LenientStubber.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/LenientStubber.kt index 1a9236bd..a9d9a340 100644 --- a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/LenientStubber.kt +++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/LenientStubber.kt @@ -28,11 +28,11 @@ package org.mockito.kotlin import org.mockito.stubbing.LenientStubber import org.mockito.stubbing.OngoingStubbing -inline fun LenientStubber.whenever(methodCall: T): OngoingStubbing { +inline fun LenientStubber.whenever(methodCall: T): OngoingStubbing { return `when`(methodCall) } -inline fun LenientStubber.whenever(methodCall: () -> T): OngoingStubbing { +inline fun LenientStubber.whenever(methodCall: () -> T): OngoingStubbing { return whenever(methodCall()) } diff --git a/tests/src/test/kotlin/test/LenientStubberTest.kt b/tests/src/test/kotlin/test/LenientStubberTest.kt index d3e67fe3..f7dddd57 100644 --- a/tests/src/test/kotlin/test/LenientStubberTest.kt +++ b/tests/src/test/kotlin/test/LenientStubberTest.kt @@ -34,4 +34,18 @@ open class LenientStubberTest { Assert.assertEquals("List should contain hello", "hello", mock[1]) } + + @Test + fun unused_and_lenient_stubbings_with_nullable() { + val mock = mock() + lenient().whenever(mock.returnsNullableString()).doReturn(null) + whenever(mock.returnsNonNullableString()).doReturn("hello") + + Assert.assertEquals("Should return hello", "hello", mock.returnsNonNullableString()) + } + + private class NullableToString { + fun returnsNullableString(): String? = "" + fun returnsNonNullableString(): String = "" + } }