CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-nhaarman-mockitokotlin2--mockito-kotlin

Helper functions to work with Mockito in Kotlin

Pending
Overview
Eval results
Files

mock-creation.mddocs/

Mock Creation

Type-safe mock creation with extensive configuration options and immediate stubbing support. Essential for setting up test doubles in Kotlin tests.

Capabilities

Basic Mock Creation

Creates a mock for the specified type with optional configuration parameters.

/**
 * Creates a mock for the specified reified type T
 * @param extraInterfaces Additional interfaces the mock should implement
 * @param name Mock name for debugging purposes
 * @param spiedInstance Instance to spy on (for partial mocks)
 * @param defaultAnswer Default answer for unstubbed methods
 * @param serializable Whether the mock should be serializable
 * @param serializableMode Specific serialization mode
 * @param verboseLogging Enable real-time method invocation logging
 * @param invocationListeners Listeners for method invocations
 * @param stubOnly Stub-only mock saves memory but disallows verification
 * @param useConstructor Attempt to use constructor when creating mock instance
 * @param outerInstance For mocking non-static inner classes with useConstructor
 * @param lenient Bypass strict stubbing validation
 * @returns Mock instance of type T
 */
inline fun <reified T : Any> mock(
    extraInterfaces: Array<out 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,
    lenient: Boolean = false
): T

Usage Examples:

import com.nhaarman.mockitokotlin2.mock

// Basic mock
val userService = mock<UserService>()

// Named mock for debugging
val namedMock = mock<UserService>(name = "userServiceMock")

// Mock with default answer
val mockWithAnswer = mock<UserService>(
    defaultAnswer = Answer { "default response" }
)

Mock with Immediate Stubbing

Creates a mock with immediate stubbing configuration using a lambda DSL.

/**
 * Creates a mock with immediate stubbing configuration
 * @param stubbing Lambda for configuring mock behavior immediately after creation
 * @returns Configured mock instance of type T
 */
inline fun <reified T : Any> mock(
    extraInterfaces: Array<out 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,
    lenient: Boolean = false,
    stubbing: KStubbing<T>.(T) -> Unit
): T

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Mock with immediate stubbing
val userService = mock<UserService> {
    on { findUser(any()) } doReturn User("John", "john@example.com")
    on { isUserActive(any()) } doReturn true
}

// Complex stubbing with multiple responses
val calculator = mock<Calculator> {
    on { add(any(), any()) } doAnswer { invocation ->
        val a = invocation.getArgument<Int>(0)
        val b = invocation.getArgument<Int>(1)
        a + b
    }
    on { divide(any(), 0) } doThrow ArithmeticException("Division by zero")
}

Mock Settings Configuration

Creates MockSettings with optional parameters for advanced mock configuration.

/**
 * Creates MockSettings with optional parameters for advanced mock configuration
 * @param extraInterfaces Additional interfaces the mock should implement
 * @param name Mock name for debugging purposes
 * @param spiedInstance Instance to spy on (for partial mocks)
 * @param defaultAnswer Default answer for unstubbed methods
 * @param serializable Whether the mock should be serializable
 * @param serializableMode Specific serialization mode
 * @param verboseLogging Enable real-time method invocation logging
 * @param invocationListeners Listeners for method invocations
 * @param stubOnly Stub-only mock saves memory but disallows verification
 * @param useConstructor Attempt to use constructor when creating mock instance
 * @param outerInstance For mocking non-static inner classes with useConstructor
 * @param lenient Bypass strict stubbing validation
 * @returns MockSettings instance for use with Mockito.mock()
 */
fun withSettings(
    extraInterfaces: Array<out 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,
    lenient: Boolean = false
): MockSettings

Usage Examples:

import com.nhaarman.mockitokotlin2.*
import org.mockito.Mockito

// Using withSettings for advanced configuration
val settings = withSettings(
    name = "advancedMock",
    verboseLogging = true,
    serializable = true
)
val mock = Mockito.mock(UserService::class.java, settings)

Constructor-Based Mocking

Helper class for constructor-based mock creation.

/**
 * Helper class for constructor-based mock creation
 */
class UseConstructor private constructor(val args: Array<Any>) {
    companion object {
        /** Creates UseConstructor for parameterless constructor invocation */
        fun parameterless(): UseConstructor
        
        /** Creates UseConstructor for constructor invocation with specified arguments */
        fun withArguments(vararg arguments: Any): UseConstructor
    }
}

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Mock using parameterless constructor
val mock = mock<SomeClass>(useConstructor = UseConstructor.parameterless())

// Mock using constructor with arguments
val mockWithArgs = mock<SomeClass>(
    useConstructor = UseConstructor.withArguments("arg1", 42, true)
)

Types

// From org.mockito
class MockSettings
class Answer<T>
class SerializableMode
class InvocationListener
class KClass<T>

// From mockito-kotlin
class UseConstructor private constructor(val args: Array<Any>) {
    companion object {
        fun parameterless(): UseConstructor
        fun withArguments(vararg arguments: Any): UseConstructor
    }
}

class KStubbing<out T>(val mock: T)

Install with Tessl CLI

npx tessl i tessl/maven-com-nhaarman-mockitokotlin2--mockito-kotlin

docs

argument-capturing.md

argument-matching.md

index.md

mock-creation.md

spying.md

stubbing.md

verification.md

tile.json