Back to Skills

unit-test-service-layer

Provides patterns for unit testing service layer with Mockito. Creates isolated tests that mock repository calls, verify method invocations, test exception scenarios, and stub external API responses. Use when testing service behaviors and business logic without database or external services.

311stars37forksUpdated 7/29/2026

Security Assessment

Safe(95/100)
Security Score95/100

About unit-test-service-layer

This skill provides patterns for unit testing Java service-layer classes with Mockito. It solves the problem of writing fast, isolated tests for business logic in `@Service` classes without spinning up a Spring container, database, or real external services. By mocking repositories and external clients, developers can verify service behavior, interactions, and error handling in milliseconds rather than through slow integration tests.

The skill lays out a clear workflow: set up the test class with `@ExtendWith(MockitoExtension.class)`, declare dependencies with `@Mock` and the class under test with `@InjectMocks`, and follow an Arrange-Act-Assert structure using `when().thenReturn()` for stubbing, AssertJ for assertions, and `verify()` for interaction checks. It covers testing exception scenarios with `when().thenThrow()`, running coverage with `mvn test jacoco:report`, and validation checkpoints at each step. A references/examples.md file adds Maven and Gradle dependency setup and patterns for multiple dependencies, argument captors, async services, and InOrder verification. It also encodes best practices and explicit constraints, such as not mocking value objects or DTOs, not testing private methods directly, keeping tests independent of execution order, and avoiding over-verification.

It targets Java and Spring developers writing unit tests, especially those working with layered service architectures who want disciplined Mockito usage. The declared allowed tools are Read, Write, Bash, Glob, and Grep, and the guidance assumes JUnit 5, Mockito, and AssertJ on the classpath.

FAQ

What testing stack does this skill assume?

JUnit 5, Mockito (with mockito-junit-jupiter), and AssertJ, wired via `@ExtendWith(MockitoExtension.class)`. It provides both Maven and Gradle dependency snippets.

What kind of tests does it help write?

Fast, isolated unit tests for `@Service` business logic that mock repository and external-client dependencies, verify interactions, and test exception and edge-case scenarios without a database or real API calls.

Are there things the skill warns against?

Yes. Do not mock value objects or DTOs (create real instances), do not test private methods directly, keep tests independent of execution order, be cautious with @Spy, and avoid over-verifying interactions.

How do I check test coverage?

Run the full suite with `mvn test` or `gradle test`, and generate a coverage report with `mvn test jacoco:report`, confirming all service methods have corresponding tests.

Does it cover advanced Mockito patterns?

The references/examples.md file adds patterns for multiple dependencies, argument captors, async services, and InOrder verification beyond the basic single-dependency example.

All Files

2 files
SKILL.md4.6 KB
View
references/examples.md9.1 KB
View

Install unit-test-service-layer

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill