File tree Expand file tree Collapse file tree 3 files changed +17
-3
lines changed
mockito-kotlin/src/main/kotlin/org/mockito/kotlin
tests/src/test/kotlin/test Expand file tree Collapse file tree 3 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -28,11 +28,12 @@ package org.mockito.kotlin
28
28
import org.mockito.kotlin.internal.createInstance
29
29
import kotlinx.coroutines.runBlocking
30
30
import org.mockito.Mockito
31
+ import org.mockito.exceptions.misusing.NotAMockException
31
32
import org.mockito.stubbing.OngoingStubbing
32
33
import kotlin.reflect.KClass
33
34
34
35
35
- inline fun <T > stubbing (
36
+ inline fun <T : Any > stubbing (
36
37
mock : T ,
37
38
stubbing : KStubbing <T >.(T ) -> Unit
38
39
) {
@@ -43,7 +44,10 @@ inline fun <T : Any> T.stub(stubbing: KStubbing<T>.(T) -> Unit): T {
43
44
return apply { KStubbing (this ).stubbing(this ) }
44
45
}
45
46
46
- class KStubbing <out T >(val mock : T ) {
47
+ class KStubbing <out T : Any >(val mock : T ) {
48
+ init {
49
+ if (! mockingDetails(mock).isMock) throw NotAMockException (" Stubbing target is not a mock!" )
50
+ }
47
51
48
52
fun <R > on (methodCall : R ): OngoingStubbing <R > = Mockito .`when `(methodCall)
49
53
Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ fun <T> spy(value: T): T {
56
56
* Creates a spy of the real object, allowing for immediate stubbing.
57
57
* The spy calls <b>real</b> methods unless they are stubbed.
58
58
*/
59
- inline fun <reified T > spy (value : T , stubbing : KStubbing <T >.(T ) -> Unit ): T {
59
+ inline fun <reified T : Any > spy (value : T , stubbing : KStubbing <T >.(T ) -> Unit ): T {
60
60
return spy(value)
61
61
.apply { KStubbing (this ).stubbing(this ) }!!
62
62
}
Original file line number Diff line number Diff line change @@ -241,6 +241,16 @@ class OngoingStubbingTest : TestBase() {
241
241
expect(mock.stringResult(" B" )).toBe(" B" )
242
242
}
243
243
244
+ @Test
245
+ fun stubbingRealObject () {
246
+ val notAMock = " "
247
+
248
+ /* Expect */
249
+ expectErrorWithMessage(" is not a mock!" ).on {
250
+ notAMock.stub { }
251
+ }
252
+ }
253
+
244
254
@Test
245
255
fun stubbingTwiceWithCheckArgumentMatchers_throwsException () {
246
256
/* Expect */
You can’t perform that action at this time.
0 commit comments