mocking
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.zipInstalls 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.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
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
| Mock | Real Object |
|---|---|
| Database/file I/O | Value objects (CustomerRecord) |
| External APIs | Pure functions |
| Email services | Simple calculations |
| Slow operations | Utility 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
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.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| mocking (this skill) | 0 | 5mo | No flags | Beginner |
| unit-testing | 3 | 3mo | No flags | Beginner |
| springboot-verification | 4 | 4mo | Review | Intermediate |
| code-checklist | 0 | 4mo | Review | Beginner |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by comeredon
View all by comeredon →You might also like
unit-testing
TencentBlueKing
单元测试编写指南,涵盖 JUnit5/MockK 使用、测试命名规范、Mock 技巧、测试覆盖率要求、TDD 实践。当用户编写单元测试、Mock 依赖、提高测试覆盖率或进行测试驱动开发时使用。
springboot-verification
affaan-m
Verification loop for Spring Boot projects: build, static analysis, tests with coverage, security scans, and diff review before release or PR.
code-checklist
comeredon
Critical code requirements checklist derived from actual build failures. Use before committing code or when troubleshooting compilation errors.
migration-unit-testing
RobertoBorges
|
springboot-tdd
affaan-m
Test-driven development for Spring Boot using JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo. Use when adding features, fixing bugs, or refactoring.
review-pr
apache
Used to review whether an Apache ShardingSphere PR truly fixes the root cause, assess side effects and regression risks, and determine whether it can be safely merged. If not mergeable, produce committer-tone change request suggestions. Supports targeted comparison across multiple review rounds.