Skip to content

Commit

Permalink
Widen allowed types for lenient().whenever() (#485)
Browse files Browse the repository at this point in the history
Fixes #480
  • Loading branch information
hunterwerlla authored Jul 11, 2023
1 parent b1d5e79 commit d92b616
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 d92b616

Please sign in to comment.