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

argument-matching.mddocs/

Argument Matching

Powerful argument matchers designed for Kotlin's type system, providing type-safe matching with reified generics and null safety. Essential for flexible verification and stubbing in Kotlin tests.

Capabilities

Basic Matchers

Core argument matchers for common matching scenarios.

/**
 * Object argument that is equal to the given value
 * @param value The value to match against
 * @returns The value (for Mockito's internal use)
 */
fun <T> eq(value: T): T

/**
 * Object argument that is the same instance as the given value
 * @param value The instance to match against
 * @returns The value (for Mockito's internal use)
 */
fun <T> same(value: T): T

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Verify with exact value match
verify(userService).findUser(eq("john123"))

// Verify with same instance match
val specificUser = User("John")
verify(userService).saveUser(same(specificUser))

Type-Safe Any Matchers

Type-safe matchers that work with Kotlin's null safety and reified generics.

/**
 * Matches any object of type T, excluding nulls
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> any(): T

/**
 * Matches anything including nulls
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> anyOrNull(): T

/**
 * Matches any vararg object, including nulls
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> anyVararg(): T

/**
 * Matches any array of type T
 * @returns A dummy array instance for Mockito's use
 */
inline fun <reified T : Any?> anyArray(): Array<T>

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Verify with any non-null argument
verify(userService).findUser(any<String>())

// Verify with any argument including null
verify(userService).findUserOptional(anyOrNull<String>())

// Stub with any array argument
whenever(dataProcessor.processItems(anyArray<String>())).doReturn(emptyList())

Custom Matchers

Create custom argument matchers with predicates for complex matching logic.

/**
 * Creates a custom argument matcher using a predicate extension function
 * Null values will never evaluate to true
 * @param predicate Extension function on T that returns true when T matches
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean): T

/**
 * Creates a custom argument matcher using an ArgumentMatcher instance
 * @param matcher The ArgumentMatcher instance to register
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> argThat(matcher: ArgumentMatcher<T>): T

/**
 * Alias for argThat - creates a custom argument matcher with predicate
 * @param predicate Extension function on T that returns true when T matches
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean): T

/**
 * Creates a custom argument matcher using a regular function predicate
 * @param predicate Function that returns true when given T matches
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> argWhere(noinline predicate: (T) -> Boolean): T

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Verify with custom predicate using extension function
verify(userService).findUser(argThat<String> { length > 5 })

// Verify with custom predicate using regular function
verify(userService).findUser(argWhere<String> { it.startsWith("user_") })

// Stub with complex matching logic
whenever(emailService.sendEmail(argThat<EmailRequest> {
    recipients.isNotEmpty() && subject.isNotBlank()
})).doReturn(true)

// Using ArgumentMatcher instance
val customMatcher = ArgumentMatcher<User> { user ->
    user.email.contains("@example.com")
}
verify(userService).saveUser(argThat(customMatcher))

Type and Null Matchers

Matchers for type checking and null/non-null verification.

/**
 * Argument that implements the given class or interface
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> isA(): T

/**
 * Matches null arguments
 * @returns null
 */
fun <T : Any> isNull(): T?

/**
 * Matches non-null arguments
 * @returns null (Mockito internal)
 */
fun <T : Any> isNotNull(): T?

/**
 * Alias for isNotNull - matches non-null arguments
 * @returns null (Mockito internal)
 */
fun <T : Any> notNull(): T?

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Verify argument implements specific type
verify(processor).handle(isA<ProcessingRequest>())

// Verify null argument
verify(userService).findUser(isNull())

// Verify non-null argument
verify(userService).findUser(isNotNull())

Reflection-Based Matching

Advanced matcher for object equality using reflection with field exclusion.

/**
 * Object argument that is reflection-equal to the given value
 * Supports excluding specific fields from comparison
 * @param value The value to compare against using reflection
 * @param excludeFields Field names to exclude from comparison
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> refEq(value: T, vararg excludeFields: String): T

Usage Examples:

import com.nhaarman.mockitokotlin2.*

val expectedUser = User(
    id = 123,
    name = "John",
    createdAt = LocalDateTime.now(),
    modifiedAt = LocalDateTime.now()
)

// Verify with reflection equality, excluding timestamp fields
verify(userService).saveUser(refEq(expectedUser, "createdAt", "modifiedAt"))

Verification-Only Matcher

Special matcher designed specifically for verification with custom validation logic.

/**
 * For usage with verification only - not for stubbing
 * Executes a predicate function to verify argument properties
 * @param predicate Function that performs validation on the argument
 * @returns A dummy instance of T for Mockito's use
 */
inline fun <reified T : Any> check(noinline predicate: (T) -> Unit): T

Usage Examples:

import com.nhaarman.mockitokotlin2.*

// Verify with custom assertions
verify(userService).saveUser(check<User> { user ->
    assertThat(user.email).isNotEmpty()
    assertThat(user.age).isGreaterThan(0)
})

// Complex verification with multiple assertions
verify(orderService).processOrder(check<Order> { order ->
    assertThat(order.items).isNotEmpty()
    assertThat(order.totalAmount).isPositive()
    assertThat(order.status).isEqualTo(OrderStatus.PENDING)
})

Types

// From org.mockito
class ArgumentMatcher<T>

// Standard Kotlin/Java types used in matchers
interface Array<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