Skip to content

Commit ea6e64c

Browse files
authored
Merge pull request #85 from nhaarman/release-0.7.0
Release 0.7.0
2 parents 9c4234e + 36e3dd5 commit ea6e64c

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

mockito-kotlin/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'kotlin'
22

33
buildscript {
4-
ext.kotlin_version = '1.0.3'
4+
ext.kotlin_version = '1.0.4'
55

66
repositories {
77
mavenCentral()
@@ -20,7 +20,7 @@ repositories {
2020
dependencies {
2121
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2222
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
23-
compile "org.mockito:mockito-core:2.1.0-RC.1"
23+
compile "org.mockito:mockito-core:2.1.0"
2424

2525
/* Tests */
2626
testCompile "junit:junit:4.12"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +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
30+
import org.mockito.internal.creation.bytebuddy.MockAccess
3131
import org.mockito.internal.util.MockUtil
3232
import java.lang.reflect.InvocationTargetException
3333
import java.lang.reflect.Modifier
@@ -172,6 +172,6 @@ private fun <T> Class<T>.uncheckedMock(): T {
172172
val impl = MockSettingsImpl<T>().defaultAnswer(Answers.RETURNS_DEFAULTS) as MockSettingsImpl<T>
173173
val creationSettings = impl.confirm(this)
174174
return MockUtil.createMock(creationSettings).apply {
175-
(this as MockMethodInterceptor.MockAccess).mockitoInterceptor = null
175+
(this as MockAccess).mockitoInterceptor = null
176176
}
177177
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ fun after(millis: Long) = Mockito.after(millis)
4141

4242
/** Matches any object, excluding nulls. */
4343
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
44+
4445
/** Matches anything, including nulls. */
4546
inline fun <reified T : Any> anyOrNull(): T = Mockito.any<T>() ?: createInstance<T>()
47+
4648
/** Matches any vararg object, including nulls. */
4749
inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance<T>()
50+
4851
/** Matches any array of type T. */
4952
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
53+
5054
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
5155
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate)
5256

@@ -80,8 +84,11 @@ inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>): T = Mockito.mock(
8084
inline fun <reified T : Any> mock(s: MockSettings): T = Mockito.mock(T::class.java, s)!!
8185
inline fun <reified T : Any> mock(s: String): T = Mockito.mock(T::class.java, s)!!
8286

83-
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.() -> Unit): T
84-
= Mockito.mock(T::class.java)!!.apply { stubbing(KStubbing(this)) }
87+
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
88+
return mock<T>().apply {
89+
KStubbing(this).stubbing(this)
90+
}
91+
}
8592

8693
class KStubbing<out T>(private val mock: T) {
8794
fun <R> on(methodCall: R) = Mockito.`when`(methodCall)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class MockitoKotlin {
7878
?.second
7979
}
8080

81-
private fun StackTraceElement.toFileIdentifier() = "$fileName$className"
81+
private fun StackTraceElement.toFileIdentifier() = "$fileName$className".let {
82+
if (it.contains("$")) it.substring(0..it.indexOf("$") - 1) else it
83+
}
8284
}
8385
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ interface Methods {
5454
fun nullableString(s: String?)
5555

5656
fun stringResult(): String
57+
fun builderMethod() : Methods
5758
}
5859

5960
class ThrowableClass(cause: Throwable) : Throwable(cause)

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
*/
2525

2626
import com.nhaarman.expect.expect
27-
import com.nhaarman.mockito_kotlin.MockitoKotlin
28-
import com.nhaarman.mockito_kotlin.createInstance
27+
import com.nhaarman.mockito_kotlin.*
28+
import org.junit.After
2929
import org.junit.Test
3030

3131
class MockitoKotlinTest {
3232

33+
@After
34+
fun teardown() {
35+
MockitoKotlin.resetInstanceCreators()
36+
}
37+
3338
@Test
3439
fun register() {
3540
/* Given */
@@ -56,4 +61,18 @@ class MockitoKotlinTest {
5661
/* Then */
5762
expect(result).toNotBeTheSameAs(closed)
5863
}
64+
65+
@Test
66+
fun usingInstanceCreatorInsideLambda() {
67+
MockitoKotlin.registerInstanceCreator { CreateInstanceTest.ForbiddenConstructor(2) }
68+
69+
mock<TestClass> {
70+
on { doSomething(any()) } doReturn ""
71+
}
72+
}
73+
74+
interface TestClass {
75+
76+
fun doSomething(c: CreateInstanceTest.ForbiddenConstructor): String
77+
}
5978
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,20 @@ class MockitoTest {
361361
expect(result).toBe("A")
362362
}
363363

364+
@Test
365+
fun testMockStubbing_builder() {
366+
/* Given */
367+
val mock = mock<Methods> { mock ->
368+
on { builderMethod() } doReturn mock
369+
}
370+
371+
/* When */
372+
val result = mock.builderMethod()
373+
374+
/* Then */
375+
expect(result).toBeTheSameAs(mock)
376+
}
377+
364378
@Test
365379
fun doReturn_withSingleItemList() {
366380
/* Given */

0 commit comments

Comments
 (0)