mockito-kotlin / com.nhaarman.mockitokotlin2

Package com.nhaarman.mockitokotlin2

Types

InOrderOnType

class InOrderOnType<T> : InOrder

KArgumentCaptor

class KArgumentCaptor<out T>

KStubbing

class KStubbing<out T>

UseConstructor

class UseConstructor

VerifyScope

class VerifyScope<out T>

Exceptions

MockitoKotlinException

class MockitoKotlinException : RuntimeException

Extensions for External Classes

org.mockito.ArgumentCaptor

org.mockito.BDDMockito.BDDMyOngoingStubbing

org.mockito.stubbing.OngoingStubbing

org.mockito.stubbing.Stubber

Functions

after

fun after(millis: Long): VerificationAfterDelay?

Allows verifying over a given period. It causes a verify to wait for a specified period of time for a desired interaction rather than failing immediately if has not already happened. May be useful for testing in concurrent conditions.

any

fun <T : Any> any(): T

Matches any object, excluding nulls.

anyArray

fun <T> anyArray(): Array<T>

Matches any array of type T.

anyOrNull

fun <T : Any> anyOrNull(): T

Matches anything, including nulls.

anyVararg

fun <T : Any> anyVararg(): T

Matches any vararg object, including nulls.

argForWhich

fun <T : Any> argForWhich(predicate: T.() -> Boolean): T

Alias for argThat.

argThat

fun <T : Any> argThat(predicate: T.() -> Boolean): T

Creates a custom argument matcher. null values will never evaluate to true.

argWhere

fun <T : Any> argWhere(predicate: (T) -> Boolean): T

Creates a custom argument matcher. null values will never evaluate to true.

argumentCaptor

fun <T : Any> argumentCaptor(): KArgumentCaptor<T>

Creates a KArgumentCaptor for given type.

fun <T : Any> argumentCaptor(f: KArgumentCaptor<T>.() -> Unit): KArgumentCaptor<T>

Creates a KArgumentCaptor for given type, taking in a lambda to allow fast verification.

atLeast

fun atLeast(numInvocations: Int): VerificationMode

Allows at-least-x verification.

atLeastOnce

fun atLeastOnce(): VerificationMode

Allows at-least-once verification.

atMost

fun atMost(maxNumberOfInvocations: Int): VerificationMode

Allows at-most-x verification.

calls

fun calls(wantedNumberOfInvocations: Int): VerificationMode

Allows non-greedy verification in order.

capture

fun <T : Any> capture(captor: ArgumentCaptor<T>): T

Alias for ArgumentCaptor.capture.

check

fun <T : Any> check(predicate: (T) -> Unit): T

For usage with verification only.

clearInvocations

fun <T> clearInvocations(vararg mocks: T): Unit

Use this method in order to only clear invocations, when stubbing is non-trivial.

description

fun description(description: String): VerificationMode

Adds a description to be printed if verification fails.

doAnswer

fun <T> doAnswer(answer: (InvocationOnMock) -> T?): Stubber

doCallRealMethod

fun doCallRealMethod(): Stubber

doNothing

fun doNothing(): Stubber

doReturn

fun doReturn(value: Any?): Stubber
fun doReturn(toBeReturned: Any?, vararg toBeReturnedNext: Any?): Stubber

doThrow

fun doThrow(toBeThrown: KClass<out Throwable>): Stubber
fun doThrow(vararg toBeThrown: Throwable): Stubber

eq

fun <T> eq(value: T): T

Object argument that is equal to the given value.

given

fun <T> given(methodCall: T): BDDMyOngoingStubbing<T>

Alias for BDDMockito.given.

fun <T> given(methodCall: () -> T): BDDMyOngoingStubbing<T>

Alias for BDDMockito.given with a lambda.

ignoreStubs

fun ignoreStubs(vararg mocks: Any): Array<out Any>

Ignores stubbed methods of given mocks for the sake of verification.

inOrder

fun inOrder(vararg mocks: Any): InOrder

Creates InOrder object that allows verifying mocks in order.

fun inOrder(vararg mocks: Any, evaluation: InOrder.() -> Unit): Unit

Creates InOrder object that allows verifying mocks in order. Accepts a lambda to allow easy evaluation.

fun <T> T.inOrder(block: InOrderOnType<T>.() -> Any): Unit

Allows InOrder verification for a single mocked instance:

isA

fun <T : Any> isA(): T

Argument that implements the given class.

isNotNull

fun <T : Any> isNotNull(): T?

Not null argument.

isNull

fun <T : Any> isNull(): T?

null argument.

mock

fun <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, useConstructor: UseConstructor? = null, outerInstance: Any? = null): T

Creates a mock for T.

fun <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, useConstructor: UseConstructor? = null, outerInstance: Any? = null, stubbing: KStubbing<T>.(T) -> Unit): T

Creates a mock for T, allowing for immediate stubbing.

fun <T : Any> mock(a: Answer<Any>): T
fun <T : Any> mock(s: String): T
fun <T : Any> mock(s: MockSettings): T

mockingDetails

fun mockingDetails(toInspect: Any): MockingDetails

never

fun never(): VerificationMode

Alias for times with parameter 0.

notNull

fun <T : Any> notNull(): T?

Not null argument.

nullableArgumentCaptor

fun <T : Any> nullableArgumentCaptor(): KArgumentCaptor<T?>

Creates a KArgumentCaptor for given nullable type.

fun <T : Any> nullableArgumentCaptor(f: KArgumentCaptor<T?>.() -> Unit): KArgumentCaptor<T?>

Creates a KArgumentCaptor for given nullable type, taking in a lambda to allow fast verification.

only

fun only(): VerificationMode

Allows checking if given method was the only one invoked.

refEq

fun <T> refEq(value: T, vararg excludeFields: String): T?

Object argument that is reflection-equal to the given value with support for excluding selected fields from a class.

reset

fun <T> reset(vararg mocks: T): Unit

same

fun <T> same(value: T): T

Object argument that is the same as the given value.

spy

fun <T : Any> spy(): T

Creates a spy of the real object. The spy calls real methods unless they are stubbed.

fun <T : Any> spy(stubbing: KStubbing<T>.(T) -> Unit): T
fun <T> spy(value: T, stubbing: KStubbing<T>.(T) -> Unit): T

Creates a spy of the real object, allowing for immediate stubbing. The spy calls real methods unless they are stubbed.

fun <T> spy(value: T): T

Creates a spy of the real object. The spy calls real methods unless they are stubbed.

stub

fun <T : Any> T.stub(stubbing: KStubbing<T>.(T) -> Unit): T

stubbing

fun <T> stubbing(mock: T, stubbing: KStubbing<T>.(T) -> Unit): Unit

then

fun <T> then(mock: T): Then<T>

Alias for BDDMockito.then.

timeout

fun timeout(millis: Long): VerificationWithTimeout

Allows verifying with timeout. It causes a verify to wait for a specified period of time for a desired interaction rather than fails immediately if has not already happened. May be useful for testing in concurrent conditions.

times

fun times(numInvocations: Int): VerificationMode

Allows verifying exact number of invocations.

validateMockitoUsage

fun validateMockitoUsage(): Unit

verify

fun <T> verify(mock: T): T

Verifies certain behavior happened once.

fun <T> verify(mock: T, mode: VerificationMode): T

Verifies certain behavior happened at least once / exact number of times / never.

fun <T> verify(mock: T, block: VerifyScope<T>.() -> Unit): Unit

Verify multiple calls on mock Supports an easier to read style of

verifyBlocking

fun <T> verifyBlocking(mock: T, f: suspend T.() -> Unit): Unit

Verifies certain suspending behavior happened once.

verifyNoMoreInteractions

fun <T> verifyNoMoreInteractions(vararg mocks: T): Unit

Checks if any of given mocks has any unverified interaction.

verifyZeroInteractions

fun verifyZeroInteractions(vararg mocks: Any): Unit

Verifies that no interactions happened on given mocks beyond the previously verified interactions.

whenever

fun <T> whenever(methodCall: T): OngoingStubbing<T>

Enables stubbing methods. Use it when you want the mock to return particular value when particular method is called.

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, useConstructor: UseConstructor? = null, outerInstance: Any? = null): MockSettings

Allows mock creation with additional mock settings. See MockSettings.