mocking-with-mockito

Configure Mockito 5.x with mockito-kotlin DSL for JVM unit tests.

303|10|Updated May 15, 2026
One-click install
npx skills add https://github.com/skydoves/android-testing-skills --skill mocking-with-mockito
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mocking-with-mockito
Source: https://github.com/skydoves/android-testing-skills/tree/main/jvm-tests/mocking/mocking-with-mockito
Command: npx skills add https://github.com/skydoves/android-testing-skills --skill mocking-with-mockito

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill solves the problem of setting up reliable Mockito-based mocking for Android JVM unit tests using the androidx-native Mockito stack and the mockito-kotlin DSL.

Core Features & Use Cases

  • Mockito + mockito-kotlin wiring for JVM tests: Adds the correct dependency matrix for Mockito 5.x alongside the Kotlin DSL, covering mock<T>(), whenever, argumentCaptor<T>(), and matcher ergonomics like any(), anyOrNull(), and eq().
  • Correct lifecycle and runner integration: Covers the three setup styles (MockitoJUnit.rule(), MockitoAnnotations.openMocks(this), and Mockito JUnit5 extension) and avoids deprecated initMocks.
  • Practical handling of common mocking failures: Fixes issues like matcher mixing (InvalidUseOfMatchersException), final-class mocking via inline mock-maker configuration, and suspend-stubbing ergonomics when using runTest or runBlocking.
  • When NOT to use it: Directs users to mockk for coroutine-style mocking (coEvery/coVerify) or to other skills for runner/coroutine setup decisions.

Quick Start

Use the mocking-with-mockito skill to configure Mockito 5.x with mockito-kotlin DSL in your JVM unit test and then stub and verify interactions using whenever, argumentCaptor<T>(), and Kotlin-safe matchers.

Frequently Asked Questions about mocking-with-mockito

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up Mockito with the mockito-kotlin DSL for JVM unit tests?▼

Configure the Mockito 5.x dependency matrix alongside mockito-kotlin DSL to enable APIs like mock<T>(), whenever, and argumentCaptor<T>() for stubbing and verifying interactions in JVM unit tests.

Why does InvalidUseOfMatchersException happen when using Kotlin matchers in Mockito?▼

InvalidUseOfMatchersException occurs from matcher mixing. Resolve this by using Kotlin-safe matchers consistently, such as any(), anyOrNull(), and eq(), ensuring all arguments use matchers rather than mixing raw values with matchers.

Can I mock final classes in Kotlin using Mockito without mockk?▼

Mock final classes in Kotlin using Mockito by configuring the inline mock-maker. This Skill provides guidance for enabling inline mock-maker for pre-5.x Mockito versions to resolve final-class mocking limitations natively.

What is the correct lifecycle setup for Mockito JUnit tests in Kotlin?▼

The correct lifecycle setup uses MockitoJUnit.rule(), MockitoAnnotations.openMocks(this), or the Mockito JUnit5 extension. This Skill covers all three styles while avoiding the deprecated initMocks method for stable test execution.

How do I stub suspend functions with Mockito in Kotlin coroutine tests?▼

Stub suspend functions safely by combining Mockito with runTest or runBlocking. This approach provides correct suspend-stubbing ergonomics for coroutine tests without requiring a separate coroutine mocking framework.

When should I not use Mockito for Kotlin mocking and choose an alternative?▼

Avoid Mockito and choose an alternative like mockk when you need coroutine-style mocking APIs such as coEvery and coVerify, or when your testing requires dedicated runner and coroutine setup decisions outside Mockito's scope.