Skip to content

Commit

Permalink
Fix issue mockito#480 by widening the allowed types for lenient().whe…
Browse files Browse the repository at this point in the history
…never()
  • Loading branch information
hunterwerlla committed Jul 10, 2023
1 parent b1d5e79 commit c8b840a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ package org.mockito.kotlin
import org.mockito.stubbing.LenientStubber
import org.mockito.stubbing.OngoingStubbing

inline fun <reified T : Any> LenientStubber.whenever(methodCall: T): OngoingStubbing<T> {
inline fun <reified T> LenientStubber.whenever(methodCall: T): OngoingStubbing<T> {
return `when`(methodCall)
}

inline fun <reified T : Any> LenientStubber.whenever(methodCall: () -> T): OngoingStubbing<T> {
inline fun <reified T> LenientStubber.whenever(methodCall: () -> T): OngoingStubbing<T> {
return whenever(methodCall())
}

14 changes: 14 additions & 0 deletions tests/src/test/kotlin/test/LenientStubberTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<NullableToString>()
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 = ""
}
}

0 comments on commit c8b840a

Please sign in to comment.