Provides Mockito mocking patterns and best practices for isolating unit tests from external dependencies.

Install

mkdir -p .claude/skills/mocking && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/10482" && unzip -o skill.zip -d .claude/skills/mocking && rm skill.zip

Installs to .claude/skills/mocking

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Mockito mocking patterns for isolating units under test. Use when testing classes with external dependencies like databases, files, or APIs.
140 chars✓ has a “when” trigger
Beginner

Key capabilities

  • Stubbing external dependencies
  • Verifying method interactions
  • Capturing method arguments
  • Isolating units under test

How it works

Uses Mockito to create mock objects for external dependencies, allowing developers to stub behaviors and verify interactions during unit tests.

Inputs & outputs

You give it
Class under test and dependency mocks
You get back
Isolated unit test results

When to use mocking

  • Stubbing database repository methods
  • Verifying interactions with external API clients
  • Testing void methods with argument captors
  • Isolating service classes from file system dependencies

About this skill

Mockito Mocking Patterns

Setup

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>5.8.0</version>
    <scope>test</scope>
</dependency>
@ExtendWith(MockitoExtension.class)
class ServiceTest {
    @Mock private CustomerRepository mockRepo;
    @InjectMocks private CustomerProcessor processor;
}

Stubbing

when(mockRepo.findById("12345")).thenReturn(testCustomer);
when(mockRepo.findById("00000")).thenThrow(new RecordNotFoundException());

Verifying Interactions

verify(mockRepo).findById("12345");
verify(mockRepo, never()).deleteById(anyString());
verify(mockRepo, times(2)).findById(anyString());

Argument Captors

@Captor ArgumentCaptor<String> captor;

verify(mockWriter).println(captor.capture());
assertEquals("expected output", captor.getValue());

When to Mock vs Use Real Objects

MockReal Object
Database/file I/OValue objects (CustomerRecord)
External APIsPure functions
Email servicesSimple calculations
Slow operationsUtility classes

Rules

  • Never mock the class under test
  • Don't over-mock — only mock external dependencies
  • Use @ExtendWith(MockitoExtension.class) for cleaner code
  • For void methods, use doThrow().when(mock).method()

When not to use it

  • When testing pure functions
  • When testing value objects
  • When the class under test is the target

Prerequisites

mockito-junit-jupiter

Limitations

  • Over-mocking can lead to brittle tests
  • Cannot mock static methods or constructors easily

How it compares

Provides a structured approach to isolation that avoids real I/O or slow operations compared to manual test setups.

Compared to similar skills

mocking side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
mocking (this skill)05moNo flagsBeginner
unit-testing33moNo flagsBeginner
springboot-verification44moReviewIntermediate
code-checklist04moReviewBeginner

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry