Skip to content

Commit 0a483e8

Browse files
authored
Add lenient option for whenever (#454)
With this, we have fully fixed #325
1 parent 17249bf commit 0a483e8

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2018 Niek Haarman
5+
* Copyright (c) 2007 Mockito contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package org.mockito.kotlin
27+
28+
import org.mockito.stubbing.LenientStubber
29+
import org.mockito.stubbing.OngoingStubbing
30+
31+
inline fun <reified T : Any> LenientStubber.whenever(methodCall: T): OngoingStubbing<T> {
32+
return `when`(methodCall)
33+
}
34+
35+
inline fun <reified T : Any> LenientStubber.whenever(methodCall: () -> T): OngoingStubbing<T> {
36+
return whenever(methodCall())
37+
}
38+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package test
2+
3+
import org.junit.Assert
4+
import org.junit.Rule
5+
import org.junit.Test
6+
import org.mockito.Mockito.lenient
7+
import org.mockito.junit.MockitoJUnit
8+
import org.mockito.junit.MockitoRule
9+
import org.mockito.kotlin.any
10+
import org.mockito.kotlin.doReturn
11+
import org.mockito.kotlin.mock
12+
import org.mockito.kotlin.whenever
13+
import org.mockito.quality.Strictness
14+
15+
16+
open class LenientStubberTest {
17+
@get:Rule
18+
val rule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS)
19+
20+
@Test
21+
fun unused_and_lenient_stubbings() {
22+
val mock = mock<MutableList<String>>()
23+
lenient().whenever(mock.add("one")).doReturn(true)
24+
whenever(mock[any()]).doReturn("hello")
25+
26+
Assert.assertEquals("List should contain hello", "hello", mock[1])
27+
}
28+
29+
@Test
30+
fun unused_and_lenient_stubbings_with_unit() {
31+
val mock = mock<MutableList<String>>()
32+
lenient().whenever { mock.add("one") }.doReturn(true)
33+
whenever(mock[any()]).doReturn("hello")
34+
35+
Assert.assertEquals("List should contain hello", "hello", mock[1])
36+
}
37+
}

0 commit comments

Comments
 (0)