From f2ed0040189b30d9c1b68e01fcf5fe46e5ee5d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Soares?= Date: Thu, 22 Sep 2016 14:38:11 -0300 Subject: [PATCH 1/5] Upgrade Kotlin to 1.0.4. --- mockito-kotlin/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index f8a150f9..5f512c27 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'kotlin' buildscript { - ext.kotlin_version = '1.0.3' + ext.kotlin_version = '1.0.4' repositories { mavenCentral() From 0886485a15e3212fee45aaedfdac06529afeacdb Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Mon, 3 Oct 2016 14:12:32 +0200 Subject: [PATCH 2/5] Update Mockito dependency to RC2 --- mockito-kotlin/build.gradle | 2 +- .../main/kotlin/com/nhaarman/mockito_kotlin/CreateInstance.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index 5f512c27..acde7e03 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -20,7 +20,7 @@ repositories { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - compile "org.mockito:mockito-core:2.1.0-RC.1" + compile "org.mockito:mockito-core:2.1.0-RC.2" /* Tests */ testCompile "junit:junit:4.12" 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 99206a8d..0ce613c9 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,7 +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.creation.bytebuddy.MockAccess import org.mockito.internal.util.MockUtil import java.lang.reflect.InvocationTargetException import java.lang.reflect.Modifier @@ -172,6 +172,6 @@ private fun Class.uncheckedMock(): T { val impl = MockSettingsImpl().defaultAnswer(Answers.RETURNS_DEFAULTS) as MockSettingsImpl val creationSettings = impl.confirm(this) return MockUtil.createMock(creationSettings).apply { - (this as MockMethodInterceptor.MockAccess).mockitoInterceptor = null + (this as MockAccess).mockitoInterceptor = null } } From e549d8d9b2622ea96e01c5834dd3f6111e9d0bde Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Mon, 3 Oct 2016 19:00:52 +0200 Subject: [PATCH 3/5] Update Mockito dependency to 2.1.0 --- mockito-kotlin/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockito-kotlin/build.gradle b/mockito-kotlin/build.gradle index acde7e03..a00aaac3 100644 --- a/mockito-kotlin/build.gradle +++ b/mockito-kotlin/build.gradle @@ -20,7 +20,7 @@ repositories { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" - compile "org.mockito:mockito-core:2.1.0-RC.2" + compile "org.mockito:mockito-core:2.1.0" /* Tests */ testCompile "junit:junit:4.12" From 0c3e137a984b38913a34415e4c617843b6c0f2ba Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Mon, 3 Oct 2016 19:09:59 +0200 Subject: [PATCH 4/5] Provide mock instance to stubbing method in mock() This will allow access to the mock inside the stubbing. For example: mock { mock -> on { method() } doReturn mock } --- .../kotlin/com/nhaarman/mockito_kotlin/Mockito.kt | 11 +++++++++-- mockito-kotlin/src/test/kotlin/Classes.kt | 1 + mockito-kotlin/src/test/kotlin/MockitoTest.kt | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt index 7b520fe6..a35ce024 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt @@ -41,12 +41,16 @@ fun after(millis: Long) = Mockito.after(millis) /** Matches any object, excluding nulls. */ inline fun any() = Mockito.any(T::class.java) ?: createInstance() + /** Matches anything, including nulls. */ inline fun anyOrNull(): T = Mockito.any() ?: createInstance() + /** Matches any vararg object, including nulls. */ inline fun anyVararg(): T = Mockito.any() ?: createInstance() + /** Matches any array of type T. */ inline fun anyArray(): Array = Mockito.any(Array::class.java) ?: arrayOf() + inline fun argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat { it -> (it as T).predicate() } ?: createInstance(T::class) inline fun argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate) @@ -80,8 +84,11 @@ inline fun mock(defaultAnswer: Answer): T = Mockito.mock( inline fun mock(s: MockSettings): T = Mockito.mock(T::class.java, s)!! inline fun mock(s: String): T = Mockito.mock(T::class.java, s)!! -inline fun mock(stubbing: KStubbing.() -> Unit): T - = Mockito.mock(T::class.java)!!.apply { stubbing(KStubbing(this)) } +inline fun mock(stubbing: KStubbing.(T) -> Unit): T { + return mock().apply { + KStubbing(this).stubbing(this) + } +} class KStubbing(private val mock: T) { fun on(methodCall: R) = Mockito.`when`(methodCall) diff --git a/mockito-kotlin/src/test/kotlin/Classes.kt b/mockito-kotlin/src/test/kotlin/Classes.kt index 9107ebc3..979c71b2 100644 --- a/mockito-kotlin/src/test/kotlin/Classes.kt +++ b/mockito-kotlin/src/test/kotlin/Classes.kt @@ -54,6 +54,7 @@ interface Methods { fun nullableString(s: String?) fun stringResult(): String + fun builderMethod() : Methods } 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 9d3a5fa4..c40f9525 100644 --- a/mockito-kotlin/src/test/kotlin/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/MockitoTest.kt @@ -361,6 +361,20 @@ class MockitoTest { expect(result).toBe("A") } + @Test + fun testMockStubbing_builder() { + /* Given */ + val mock = mock { mock -> + on { builderMethod() } doReturn mock + } + + /* When */ + val result = mock.builderMethod() + + /* Then */ + expect(result).toBeTheSameAs(mock) + } + @Test fun doReturn_withSingleItemList() { /* Given */ From 1d65fc06636aa2a191d5375b76a2a9bb98bfc630 Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Mon, 3 Oct 2016 19:19:03 +0200 Subject: [PATCH 5/5] Strip methods from file identifiers. --- .../nhaarman/mockito_kotlin/MockitoKotlin.kt | 4 +++- .../src/test/kotlin/MockitoKotlinTest.kt | 23 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/MockitoKotlin.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/MockitoKotlin.kt index 2158d7a9..6c07cf62 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/MockitoKotlin.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/MockitoKotlin.kt @@ -78,6 +78,8 @@ class MockitoKotlin { ?.second } - private fun StackTraceElement.toFileIdentifier() = "$fileName$className" + private fun StackTraceElement.toFileIdentifier() = "$fileName$className".let { + if (it.contains("$")) it.substring(0..it.indexOf("$") - 1) else it + } } } diff --git a/mockito-kotlin/src/test/kotlin/MockitoKotlinTest.kt b/mockito-kotlin/src/test/kotlin/MockitoKotlinTest.kt index 3b5774f6..b0465d1c 100644 --- a/mockito-kotlin/src/test/kotlin/MockitoKotlinTest.kt +++ b/mockito-kotlin/src/test/kotlin/MockitoKotlinTest.kt @@ -24,12 +24,17 @@ */ import com.nhaarman.expect.expect -import com.nhaarman.mockito_kotlin.MockitoKotlin -import com.nhaarman.mockito_kotlin.createInstance +import com.nhaarman.mockito_kotlin.* +import org.junit.After import org.junit.Test class MockitoKotlinTest { + @After + fun teardown() { + MockitoKotlin.resetInstanceCreators() + } + @Test fun register() { /* Given */ @@ -56,4 +61,18 @@ class MockitoKotlinTest { /* Then */ expect(result).toNotBeTheSameAs(closed) } + + @Test + fun usingInstanceCreatorInsideLambda() { + MockitoKotlin.registerInstanceCreator { CreateInstanceTest.ForbiddenConstructor(2) } + + mock { + on { doSomething(any()) } doReturn "" + } + } + + interface TestClass { + + fun doSomething(c: CreateInstanceTest.ForbiddenConstructor): String + } }