Skip to content

Commit c8d9f68

Browse files
committed
Unset the MockitoInterceptor for unchecked mocks.
This allows the default functions to be called on these mocks during verification. Apparently, the MockitoInterceptor added by Mockito does some internal state checking. This is not necessary here.
1 parent 129f9e6 commit c8d9f68

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package com.nhaarman.mockito_kotlin
2727

2828
import org.mockito.Answers
2929
import org.mockito.internal.creation.MockSettingsImpl
30+
import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor
3031
import org.mockito.internal.util.MockUtil
3132
import java.lang.reflect.Modifier
3233
import java.lang.reflect.ParameterizedType
@@ -160,5 +161,7 @@ private fun <T : Any> KType.createNullableInstance(): T? {
160161
private fun <T> Class<T>.uncheckedMock(): T {
161162
val impl = MockSettingsImpl<T>().defaultAnswer(Answers.RETURNS_DEFAULTS) as MockSettingsImpl<T>
162163
val creationSettings = impl.confirm(this)
163-
return MockUtil().createMock(creationSettings)
164+
return MockUtil().createMock(creationSettings).apply {
165+
(this as MockMethodInterceptor.MockAccess).mockitoInterceptor = null
166+
}
164167
}

mockito-kotlin/src/test/kotlin/Classes.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ interface Methods {
4949
fun closedSet(s: Set<Closed>)
5050
fun string(s: String)
5151
fun closedVararg(vararg c: Closed)
52+
fun throwableClass(t: ThrowableClass)
5253

5354
fun stringResult(): String
54-
}
55+
}
56+
57+
class ThrowableClass(cause: Throwable) : Throwable(cause)

mockito-kotlin/src/test/kotlin/MockitoTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import com.nhaarman.expect.expectErrorWithMessage
33
import com.nhaarman.mockito_kotlin.*
44
import org.junit.Test
55
import org.mockito.exceptions.base.MockitoAssertionError
6+
import java.io.IOException
67

78
/*
89
* The MIT License
@@ -107,6 +108,14 @@ class MockitoTest {
107108
}
108109
}
109110

111+
@Test
112+
fun anyThrowableWithSingleThrowableConstructor() {
113+
mock<Methods>().apply {
114+
throwableClass(ThrowableClass(IOException()))
115+
verify(this).throwableClass(any())
116+
}
117+
}
118+
110119
@Test
111120
fun listArgThat() {
112121
mock<Methods>().apply {

0 commit comments

Comments
 (0)