From d92b6163855c0d28d55226e1cdbcf1c4bb6126b9 Mon Sep 17 00:00:00 2001 From: Hunter Werlla Date: Mon, 10 Jul 2023 22:16:14 -0700 Subject: [PATCH] Widen allowed types for lenient().whenever() (#485) Fixes #480 --- .../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 = "" + } }