Skip to content

Commit

Permalink
Merge pull request #150 from nhaarman/release-1.2.0
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
nhaarman committed Jan 21, 2017
2 parents 3dbe0d0 + 26a139f commit 6330fe3
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 208 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ matrix:
- jdk: oraclejdk7
env: TERM=dumb MOCK_MAKER=mock-maker-inline KOTLIN_VERSION=1.0.6
- jdk: oraclejdk7
env: TERM=dumb MOCK_MAKER=mock-maker-inline KOTLIN_VERSION=1.1-M04
env: TERM=dumb MOCK_MAKER=mock-maker-inline KOTLIN_VERSION=1.1.0-beta-18
- jdk: oraclejdk8
env: TERM=dumb KOTLIN_VERSION=1.0.6
- jdk: oraclejdk8
env: TERM=dumb KOTLIN_VERSION=1.1-M04
env: TERM=dumb KOTLIN_VERSION=1.1.0-beta-18


env:
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6 changes: 3 additions & 3 deletions mockito-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.11"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.13"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3"
classpath "com.github.dcendents:android-maven-gradle-plugin:1.5"
}
Expand All @@ -28,11 +28,11 @@ repositories {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "org.mockito:mockito-core:2.4.5"
compile "org.mockito:mockito-core:2.6.5"

/* Tests */
testCompile "junit:junit:4.12"
testCompile "com.nhaarman:expect.kt:0.6.0"
testCompile "com.nhaarman:expect.kt:0.6.2"
}

dokka {
Expand Down
109 changes: 94 additions & 15 deletions mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
package com.nhaarman.mockito_kotlin

import com.nhaarman.mockito_kotlin.createinstance.createInstance
import org.mockito.InOrder
import org.mockito.MockSettings
import org.mockito.MockingDetails
import org.mockito.Mockito
import org.mockito.*
import org.mockito.invocation.InvocationOnMock
import org.mockito.listeners.InvocationListener
import org.mockito.mock.SerializableMode
import org.mockito.stubbing.Answer
import org.mockito.stubbing.OngoingStubbing
import org.mockito.stubbing.Stubber
import org.mockito.verification.VerificationMode
import org.mockito.verification.VerificationWithTimeout
import kotlin.DeprecationLevel.WARNING
import kotlin.reflect.KClass

fun after(millis: Long) = Mockito.after(millis)
Expand Down Expand Up @@ -74,7 +74,7 @@ inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean) =
*
* @param predicate A function that returns `true` when given [T] matches the predicate.
*/
inline fun <reified T: Any> argWhere(noinline predicate: (T) -> Boolean) = argThat(predicate)
inline fun <reified T : Any> argWhere(noinline predicate: (T) -> Boolean) = argThat(predicate)

/**
* For usage with verification only.
Expand Down Expand Up @@ -120,16 +120,70 @@ inline fun <reified T : Any> isA(): T = Mockito.isA(T::class.java) ?: createInst
fun <T : Any> isNotNull(): T? = Mockito.isNotNull()
fun <T : Any> isNull(): T? = Mockito.isNull()

inline fun <reified T : Any> mock(): T = Mockito.mock(T::class.java)!!
inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>): T = Mockito.mock(T::class.java, defaultAnswer)!!
inline fun <reified T : Any> mock(
extraInterfaces: Array<KClass<out Any>>? = null,
name: String? = null,
spiedInstance: Any? = null,
defaultAnswer: Answer<Any>? = null,
serializable: Boolean = false,
serializableMode: SerializableMode? = null,
verboseLogging: Boolean = false,
invocationListeners: Array<InvocationListener>? = null,
stubOnly: Boolean = false,
@Incubating useConstructor: Boolean = false,
@Incubating outerInstance: Any? = null
): T = Mockito.mock(T::class.java, withSettings(
extraInterfaces = extraInterfaces,
name = name,
spiedInstance = spiedInstance,
defaultAnswer = defaultAnswer,
serializable = serializable,
serializableMode = serializableMode,
verboseLogging = verboseLogging,
invocationListeners = invocationListeners,
stubOnly = stubOnly,
useConstructor = useConstructor,
outerInstance = outerInstance
))!!

inline fun <reified T : Any> mock(
extraInterfaces: Array<KClass<out Any>>? = null,
name: String? = null,
spiedInstance: Any? = null,
defaultAnswer: Answer<Any>? = null,
serializable: Boolean = false,
serializableMode: SerializableMode? = null,
verboseLogging: Boolean = false,
invocationListeners: Array<InvocationListener>? = null,
stubOnly: Boolean = false,
@Incubating useConstructor: Boolean = false,
@Incubating outerInstance: Any? = null,
stubbing: KStubbing<T>.(T) -> Unit
): T = Mockito.mock(T::class.java, withSettings(
extraInterfaces = extraInterfaces,
name = name,
spiedInstance = spiedInstance,
defaultAnswer = defaultAnswer,
serializable = serializable,
serializableMode = serializableMode,
verboseLogging = verboseLogging,
invocationListeners = invocationListeners,
stubOnly = stubOnly,
useConstructor = useConstructor,
outerInstance = outerInstance
)).apply {
KStubbing(this).stubbing(this)
}!!

@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock<T>(defaultAnswer = a)"), level = WARNING)
inline fun <reified T : Any> mock(a: Answer<Any>): T = mock(defaultAnswer = a)

@Deprecated("Use mock() with optional arguments instead.", ReplaceWith("mock<T>(name = s)"), level = WARNING)
inline fun <reified T : Any> mock(s: String): T = mock(name = s)

@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Use mock() with optional arguments instead.", level = WARNING)
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>.(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 Expand Up @@ -193,4 +247,29 @@ fun <T> verifyNoMoreInteractions(vararg mocks: T) = Mockito.verifyNoMoreInteract
fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*mocks)

fun <T> whenever(methodCall: T): OngoingStubbing<T> = Mockito.`when`(methodCall)!!
fun withSettings(): MockSettings = Mockito.withSettings()!!

fun withSettings(
extraInterfaces: Array<KClass<out Any>>? = null,
name: String? = null,
spiedInstance: Any? = null,
defaultAnswer: Answer<Any>? = null,
serializable: Boolean = false,
serializableMode: SerializableMode? = null,
verboseLogging: Boolean = false,
invocationListeners: Array<InvocationListener>? = null,
stubOnly: Boolean = false,
@Incubating useConstructor: Boolean = false,
@Incubating outerInstance: Any? = null
): MockSettings = Mockito.withSettings().apply {
extraInterfaces?.let { extraInterfaces(*it.map { it.java }.toTypedArray()) }
name?.let { name(it) }
spiedInstance?.let { spiedInstance(it) }
defaultAnswer?.let { defaultAnswer(it) }
if (serializable) serializable()
serializableMode?.let { serializable(it) }
if (verboseLogging) verboseLogging()
invocationListeners?.let { invocationListeners(*it) }
if (stubOnly) stubOnly()
if (useConstructor) useConstructor()
outerInstance?.let { outerInstance(it) }
}
11 changes: 11 additions & 0 deletions mockito-kotlin/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ interface Methods {
fun stringResult(s: String): String
fun nullableStringResult(): String?
fun builderMethod(): Methods

fun nonDefaultReturnType(): ExtraInterface
}

interface ExtraInterface

abstract class ThrowingConstructor {

constructor() {
error("Error in constructor")
}
}

interface GenericMethods<T> {
Expand Down
Loading

0 comments on commit 6330fe3

Please sign in to comment.