Skip to content

Commit

Permalink
Merge pull request #85 from nhaarman/release-0.7.0
Browse files Browse the repository at this point in the history
Release 0.7.0
  • Loading branch information
nhaarman authored Oct 3, 2016
2 parents 9c4234e + 36e3dd5 commit ea6e64c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
4 changes: 2 additions & 2 deletions mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'kotlin'

buildscript {
ext.kotlin_version = '1.0.3'
ext.kotlin_version = '1.0.4'

repositories {
mavenCentral()
Expand All @@ -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"

/* Tests */
testCompile "junit:junit:4.12"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -172,6 +172,6 @@ private fun <T> Class<T>.uncheckedMock(): T {
val impl = MockSettingsImpl<T>().defaultAnswer(Answers.RETURNS_DEFAULTS) as MockSettingsImpl<T>
val creationSettings = impl.confirm(this)
return MockUtil.createMock(creationSettings).apply {
(this as MockMethodInterceptor.MockAccess).mockitoInterceptor = null
(this as MockAccess).mockitoInterceptor = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ fun after(millis: Long) = Mockito.after(millis)

/** Matches any object, excluding nulls. */
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()

/** Matches anything, including nulls. */
inline fun <reified T : Any> anyOrNull(): T = Mockito.any<T>() ?: createInstance<T>()

/** Matches any vararg object, including nulls. */
inline fun <reified T : Any> anyVararg(): T = Mockito.any<T>() ?: createInstance<T>()

/** Matches any array of type T. */
inline fun <reified T : Any?> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()

inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(T::class)
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) = argThat(predicate)

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

inline fun <reified T : Any> mock(stubbing: KStubbing<T>.() -> Unit): T
= Mockito.mock(T::class.java)!!.apply { stubbing(KStubbing(this)) }
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.(T) -> Unit): T {
return mock<T>().apply {
KStubbing(this).stubbing(this)
}
}

class KStubbing<out T>(private val mock: T) {
fun <R> on(methodCall: R) = Mockito.`when`(methodCall)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
1 change: 1 addition & 0 deletions mockito-kotlin/src/test/kotlin/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface Methods {
fun nullableString(s: String?)

fun stringResult(): String
fun builderMethod() : Methods
}

class ThrowableClass(cause: Throwable) : Throwable(cause)
23 changes: 21 additions & 2 deletions mockito-kotlin/src/test/kotlin/MockitoKotlinTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -56,4 +61,18 @@ class MockitoKotlinTest {
/* Then */
expect(result).toNotBeTheSameAs(closed)
}

@Test
fun usingInstanceCreatorInsideLambda() {
MockitoKotlin.registerInstanceCreator { CreateInstanceTest.ForbiddenConstructor(2) }

mock<TestClass> {
on { doSomething(any()) } doReturn ""
}
}

interface TestClass {

fun doSomething(c: CreateInstanceTest.ForbiddenConstructor): String
}
}
14 changes: 14 additions & 0 deletions mockito-kotlin/src/test/kotlin/MockitoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,20 @@ class MockitoTest {
expect(result).toBe("A")
}

@Test
fun testMockStubbing_builder() {
/* Given */
val mock = mock<Methods> { mock ->
on { builderMethod() } doReturn mock
}

/* When */
val result = mock.builderMethod()

/* Then */
expect(result).toBeTheSameAs(mock)
}

@Test
fun doReturn_withSingleItemList() {
/* Given */
Expand Down

0 comments on commit ea6e64c

Please sign in to comment.