Skip to content

Commit

Permalink
Include more static Mockito methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Mar 10, 2016
1 parent ce93783 commit 5105493
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 61 deletions.
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-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,9 @@
* THE SOFTWARE.
*/

import com.nhaarman.mockito_kotlin.argThat
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import org.junit.Test
package com.nhaarman.mockito_kotlin

class MatcherTest {
import org.mockito.ArgumentCaptor

@Test
fun argThat() {
/* Given */
val testClass: TestClass = mock()

/* When */
testClass.go(listOf("test"))

/* Then */
verify(testClass).go(
argThat {
size == 1
get(0) == "test"
}
)
}

interface TestClass {

fun go(v: List<String>)
}
}
inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)
inline fun <reified T : Any> capture(captor: ArgumentCaptor<T>): T = captor.capture() ?: createInstance<T>()
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private fun <T : Any> KClass<T>.toArrayInstance(): T {
"LongArray" -> longArrayOf()
"DoubleArray" -> doubleArrayOf()
"FloatArray" -> floatArrayOf()
else -> throw UnsupportedOperationException("Cannot create a generic array for $simpleName. Use createArrayInstance() instead.")
else -> throw UnsupportedOperationException("Cannot create a generic array for $simpleName. Use createArrayInstance() or anyArray() instead.")
} as T
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,78 @@

package com.nhaarman.mockito_kotlin

import org.mockito.ArgumentCaptor
import org.mockito.MockSettings
import org.mockito.Mockito
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import org.mockito.stubbing.Stubber
import org.mockito.verification.VerificationMode
import kotlin.reflect.KClass

inline fun <reified T : Any> mock() = Mockito.mock(T::class.java)
inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>) = Mockito.mock(T::class.java, defaultAnswer)
fun <T : Any> spy(value: T) = Mockito.spy(value)
fun after(millis: Long) = Mockito.after(millis)

fun <T> whenever(methodCall: T) = Mockito.`when`(methodCall)
fun <T> verify(mock: T) = Mockito.verify(mock)
fun <T> verify(mock: T, mode: VerificationMode) = Mockito.verify(mock, mode)
fun <T> verifyNoMoreInteractions(mock: T) = Mockito.verifyNoMoreInteractions(mock)
fun <T> reset(mock: T) = Mockito.reset(mock)
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
inline fun <reified T : Any> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
inline fun <reified T : Any> anyCollection(): Collection<T> = Mockito.anyCollectionOf(T::class.java)
inline fun <reified T : Any> anyList(): List<T> = Mockito.anyListOf(T::class.java)
inline fun <reified T : Any> anySet(): Set<T> = Mockito.anySetOf(T::class.java)
inline fun <reified K : Any, reified V : Any> anyMap(): Map<K, V> = Mockito.anyMapOf(K::class.java, V::class.java)
inline fun <reified T : Any> anyVararg() = Mockito.anyVararg<T>() ?: createInstance<T>()

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

fun inOrder(vararg value: Any) = Mockito.inOrder(*value)
fun never() = Mockito.never()
fun times(numInvocations: Int) = Mockito.times(numInvocations)
fun atLeast(numInvocations: Int) = Mockito.atLeast(numInvocations)
fun atLeastOnce() = Mockito.atLeastOnce()
fun atMost(maxNumberOfInvocations: Int) = Mockito.atMost(maxNumberOfInvocations)
fun calls(wantedNumberOfInvocations: Int) = Mockito.calls(wantedNumberOfInvocations)

fun doReturn(value: Any) = Mockito.doReturn(value)
fun doThrow(throwable: Throwable) = Mockito.doThrow(throwable)
fun <T> doAnswer(answer: Answer<T>) = Mockito.doAnswer(answer)
fun doCallRealMethod() = Mockito.doCallRealMethod()
fun doNothing() = Mockito.doNothing()
fun <T> clearInvocations(vararg mocks: T) = Mockito.clearInvocations(*mocks)
fun description(description: String) = Mockito.description(description)

fun <T> Stubber.whenever(mock: T) = `when`(mock)
fun <T> doAnswer(answer: (InvocationOnMock) -> T?) = Mockito.doAnswer { answer(it) }

inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)
inline fun <reified T : Any> capture(captor: ArgumentCaptor<T>): T = captor.capture() ?: createInstance<T>()
fun doCallRealMethod() = Mockito.doCallRealMethod()
fun doNothing() = Mockito.doNothing()
fun doReturn(value: Any) = Mockito.doReturn(value)
fun doReturn(toBeReturned: Any, vararg toBeReturnedNext: Any) = Mockito.doReturn(toBeReturned, *toBeReturnedNext)
fun doThrow(toBeThrown: KClass<out Throwable>) = Mockito.doThrow(toBeThrown.java)
fun doThrow(vararg toBeThrown: Throwable) = Mockito.doThrow(*toBeThrown)

inline fun <reified T : Any> eq(value: T) = Mockito.eq(value) ?: createInstance<T>()
inline fun <reified T : Any> anyArray(): Array<T> = Mockito.any(Array<T>::class.java) ?: arrayOf()
inline fun <reified T : Any> any() = Mockito.any(T::class.java) ?: createInstance<T>()
fun ignoreStubs(vararg mocks: Any) = Mockito.ignoreStubs(*mocks)
fun inOrder(vararg mocks: Any) = Mockito.inOrder(*mocks)

inline fun <reified T : Any> isA() = Mockito.isA(T::class.java)
inline fun <reified T : Any> isNotNull() = Mockito.isNotNull(T::class.java)
inline fun <reified T : Any> isNull(): T? = Mockito.isNull(T::class.java)

inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean) = argThat(T::class, predicate)
inline fun <reified T : Any> mock() = Mockito.mock(T::class.java)
inline fun <reified T : Any> mock(defaultAnswer: Answer<Any>) = Mockito.mock(T::class.java, defaultAnswer)
inline fun <reified T : Any> mock(s: MockSettings) = Mockito.mock(T::class.java, s)
inline fun <reified T : Any> mock(s: String) = Mockito.mock(T::class.java, s)

fun mockingDetails(toInspect: Any) = Mockito.mockingDetails(toInspect)
fun never() = Mockito.never()
inline fun <reified T : Any> notNull() = Mockito.notNull(T::class.java)
fun only() = Mockito.only()
fun <T> refEq(value: T, vararg excludeFields: String) = Mockito.refEq(value, *excludeFields)

fun reset() = Mockito.reset<Any>()
fun <T> reset(vararg mocks: T) = Mockito.reset(*mocks)

@Suppress("UNCHECKED_CAST")
fun <T : Any> argThat(kClass: KClass<T>, predicate: T.() -> Boolean)
= Mockito.argThat<T> { it -> (it as T).predicate() } ?: createInstance(kClass)
fun <T> same(value: T) = Mockito.same(value)

inline fun <reified T : Any> spy() = Mockito.spy(T::class.java)
fun <T> spy(value: T) = Mockito.spy(value)

fun <T> stub(methodCall: T) = Mockito.stub(methodCall)
fun timeout(millis: Long) = Mockito.timeout(millis)
fun times(numInvocations: Int) = Mockito.times(numInvocations)
fun validateMockitoUsage() = Mockito.validateMockitoUsage()

fun <T> verify(mock: T) = Mockito.verify(mock)
fun <T> verify(mock: T, mode: VerificationMode) = Mockito.verify(mock, mode)
fun <T> verifyNoMoreInteractions(vararg mocks: T) = Mockito.verifyNoMoreInteractions(*mocks)
fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*mocks)

fun <T> whenever(methodCall: T) = Mockito.`when`(methodCall)
fun withSettings() = Mockito.withSettings()
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License
*
* Copyright (c) 2016 Niek Haarman
* Copyright (c) 2007 Mockito contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.nhaarman.mockito_kotlin

import org.mockito.stubbing.Stubber

fun <T> Stubber.whenever(mock: T) = `when`(mock)
1 change: 1 addition & 0 deletions mockito-kotlin/src/test/kotlin/ArgumentCaptorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.junit.Test
import java.util.*

class ArgumentCaptorTest {

@Test
fun captor() {
val date: Date = mock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,32 @@
* THE SOFTWARE.
*/

open class Fake {
open fun go(arg: Any?) {
open class Open {
open fun go(vararg arg: Any?) {
}

open fun modifiesContents(a: IntArray) {
for (i in 0..a.size - 1) {
a[i] = a[i] + 1
}
}

open fun stringResult() = "Default"
}

class Closed

interface Methods {

fun intArray(i: IntArray)
fun closed(c: Closed)
fun closedArray(a: Array<Closed>)
fun closedCollection(c: Collection<Closed>)
fun closedList(c: List<Closed>)
fun closedStringMap(m: Map<Closed, String>)
fun closedSet(s: Set<Closed>)
fun string(s: String)
fun closedVararg(vararg c: Closed)

fun stringResult(): String
}
4 changes: 2 additions & 2 deletions mockito-kotlin/src/test/kotlin/CreateInstanceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class CreateInstanceTest {
@Test(expected = UnsupportedOperationException::class)
fun classArray_usingAny() {
/* When */
createInstance<Array<Fake>>()
createInstance<Array<Open>>()
}

@Test
Expand Down Expand Up @@ -371,7 +371,7 @@ class CreateInstanceTest {
private class PrivateClass private constructor(val data: String)

class ClosedClass
class ClosedParameterizedClass(val fake: Fake)
class ClosedParameterizedClass(val open: Open)
class ClosedClosedParameterizedClass(val closed: ClosedParameterizedClass)

class SingleParameterClass(val first: Byte)
Expand Down
2 changes: 1 addition & 1 deletion mockito-kotlin/src/test/kotlin/EqTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EqTest {
private val openClassInstance: MyClass = MyClass()
private val closedClassInstance: ClosedClass = ClosedClass()

private lateinit var doAnswer: Fake
private lateinit var doAnswer: Open

@Before
fun setup() {
Expand Down
Loading

0 comments on commit 5105493

Please sign in to comment.