com.nhaarman.mockito_kotlin.Mockito.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-kotlin Show documentation
Show all versions of mockito-kotlin Show documentation
Using Mockito with Kotlin.
/*
* 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 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.invocation.InvocationOnMock
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.reflect.KClass
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)
inline fun check(noinline predicate: (T) -> Unit) = Mockito.argThat { it -> predicate(it); true } ?: createInstance(T::class)
fun atLeast(numInvocations: Int): VerificationMode = Mockito.atLeast(numInvocations)!!
fun atLeastOnce(): VerificationMode = Mockito.atLeastOnce()!!
fun atMost(maxNumberOfInvocations: Int): VerificationMode = Mockito.atMost(maxNumberOfInvocations)!!
fun calls(wantedNumberOfInvocations: Int): VerificationMode = Mockito.calls(wantedNumberOfInvocations)!!
fun clearInvocations(vararg mocks: T) = Mockito.clearInvocations(*mocks)
fun description(description: String): VerificationMode = Mockito.description(description)
fun doAnswer(answer: (InvocationOnMock) -> T?): Stubber = Mockito.doAnswer { answer(it) }!!
fun doCallRealMethod(): Stubber = Mockito.doCallRealMethod()!!
fun doNothing(): Stubber = Mockito.doNothing()!!
fun doReturn(value: Any?): Stubber = Mockito.doReturn(value)!!
fun doReturn(toBeReturned: Any?, vararg toBeReturnedNext: Any?): Stubber = Mockito.doReturn(toBeReturned, *toBeReturnedNext)!!
fun doThrow(toBeThrown: KClass): Stubber = Mockito.doThrow(toBeThrown.java)!!
fun doThrow(vararg toBeThrown: Throwable): Stubber = Mockito.doThrow(*toBeThrown)!!
fun eq(value: T): T = Mockito.eq(value) ?: value
fun ignoreStubs(vararg mocks: Any): Array = Mockito.ignoreStubs(*mocks)!!
fun inOrder(vararg mocks: Any): InOrder = Mockito.inOrder(*mocks)!!
fun inOrder(vararg mocks: Any, evaluation: InOrder.() -> Unit) = Mockito.inOrder(*mocks).evaluation()
inline fun isA(): T = Mockito.isA(T::class.java) ?: createInstance()
fun isNotNull(): T? = Mockito.isNotNull()
fun isNull(): T? = Mockito.isNull()
inline fun mock(): T = Mockito.mock(T::class.java)!!
inline fun mock(defaultAnswer: Answer): T = Mockito.mock(T::class.java, defaultAnswer)!!
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.(T) -> Unit): T {
return mock().apply {
KStubbing(this).stubbing(this)
}
}
class KStubbing(private val mock: T) {
fun on(methodCall: R) = Mockito.`when`(methodCall)
fun onGeneric(methodCall: T.() -> R, c: KClass): OngoingStubbing {
val r = try {
mock.methodCall()
} catch(e: NullPointerException) {
// An NPE may be thrown by the Kotlin type system when the MockMethodInterceptor returns a
// null value for a non-nullable generic type.
// We catch this NPE to return a valid instance.
// The Mockito state has already been modified at this point to reflect
// the wanted changes.
createInstance(c)
}
return Mockito.`when`(r)
}
inline fun onGeneric(noinline methodCall: T.() -> R): OngoingStubbing {
return onGeneric(methodCall, R::class)
}
fun on(methodCall: T.() -> R): OngoingStubbing {
return try {
Mockito.`when`(mock.methodCall())
} catch(e: NullPointerException) {
throw MockitoKotlinException("NullPointerException thrown when stubbing. If you are trying to stub a generic method, try `onGeneric` instead.", e)
}
}
}
infix fun OngoingStubbing.doReturn(t: T): OngoingStubbing = thenReturn(t)
fun OngoingStubbing.doReturn(t: T, vararg ts: T): OngoingStubbing = thenReturn(t, *ts)
inline infix fun OngoingStubbing.doReturn(ts: List): OngoingStubbing = thenReturn(ts[0], *ts.drop(1).toTypedArray())
infix fun OngoingStubbing.doThrow(t: Throwable): OngoingStubbing = thenThrow(t)
fun OngoingStubbing.doThrow(t: Throwable, vararg ts: Throwable): OngoingStubbing = thenThrow(t, *ts)
infix fun OngoingStubbing.doThrow(t: KClass): OngoingStubbing = thenThrow(t.java)
fun OngoingStubbing.doThrow(t: KClass, vararg ts: KClass): OngoingStubbing = thenThrow(t.java, *ts.map { it.java }.toTypedArray())
fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
fun never(): VerificationMode = Mockito.never()!!
fun notNull(): T? = Mockito.notNull()
fun only(): VerificationMode = Mockito.only()!!
fun refEq(value: T, vararg excludeFields: String): T? = Mockito.refEq(value, *excludeFields)
fun reset(vararg mocks: T) = Mockito.reset(*mocks)
fun same(value: T): T? = Mockito.same(value) ?: value
inline fun spy(): T = Mockito.spy(T::class.java)!!
fun spy(value: T): T = Mockito.spy(value)!!
fun timeout(millis: Long): VerificationWithTimeout = Mockito.timeout(millis)!!
fun times(numInvocations: Int): VerificationMode = Mockito.times(numInvocations)!!
fun validateMockitoUsage() = Mockito.validateMockitoUsage()
fun verify(mock: T): T = Mockito.verify(mock)!!
fun verify(mock: T, mode: VerificationMode): T = Mockito.verify(mock, mode)!!
fun verifyNoMoreInteractions(vararg mocks: T) = Mockito.verifyNoMoreInteractions(*mocks)
fun verifyZeroInteractions(vararg mocks: Any) = Mockito.verifyZeroInteractions(*mocks)
fun whenever(methodCall: T): OngoingStubbing = Mockito.`when`(methodCall)!!
fun withSettings(): MockSettings = Mockito.withSettings()!!