Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #480 by widening the allowed types for lenient().whenever() #485

Merged
merged 1 commit into from Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 = ""
}
}