From c8d9f689563eae6331ba21fa80d54f14eff159c0 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Sun, 27 Mar 2016 16:00:41 +0200 Subject: [PATCH] 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. --- .../kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt | 5 ++++- mockito-kotlin/src/test/kotlin/Classes.kt | 5 ++++- mockito-kotlin/src/test/kotlin/MockitoTest.kt | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt index 8a7cb300..3bc508a4 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt @@ -27,6 +27,7 @@ package com.nhaarman.mockito_kotlin import org.mockito.Answers import org.mockito.internal.creation.MockSettingsImpl +import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor import org.mockito.internal.util.MockUtil import java.lang.reflect.Modifier import java.lang.reflect.ParameterizedType @@ -160,5 +161,7 @@ private fun KType.createNullableInstance(): T? { private fun Class.uncheckedMock(): T { val impl = MockSettingsImpl().defaultAnswer(Answers.RETURNS_DEFAULTS) as MockSettingsImpl val creationSettings = impl.confirm(this) - return MockUtil().createMock(creationSettings) + return MockUtil().createMock(creationSettings).apply { + (this as MockMethodInterceptor.MockAccess).mockitoInterceptor = null + } } diff --git a/mockito-kotlin/src/test/kotlin/Classes.kt b/mockito-kotlin/src/test/kotlin/Classes.kt index f0ff2dfd..d494872c 100644 --- a/mockito-kotlin/src/test/kotlin/Classes.kt +++ b/mockito-kotlin/src/test/kotlin/Classes.kt @@ -49,6 +49,9 @@ interface Methods { fun closedSet(s: Set) fun string(s: String) fun closedVararg(vararg c: Closed) + fun throwableClass(t: ThrowableClass) fun stringResult(): String -} \ No newline at end of file +} + +class ThrowableClass(cause: Throwable) : Throwable(cause) diff --git a/mockito-kotlin/src/test/kotlin/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/MockitoTest.kt index 394594b2..ef635b33 100644 --- a/mockito-kotlin/src/test/kotlin/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/MockitoTest.kt @@ -3,6 +3,7 @@ import com.nhaarman.expect.expectErrorWithMessage import com.nhaarman.mockito_kotlin.* import org.junit.Test import org.mockito.exceptions.base.MockitoAssertionError +import java.io.IOException /* * The MIT License @@ -107,6 +108,14 @@ class MockitoTest { } } + @Test + fun anyThrowableWithSingleThrowableConstructor() { + mock().apply { + throwableClass(ThrowableClass(IOException())) + verify(this).throwableClass(any()) + } + } + @Test fun listArgThat() { mock().apply {