New advertisers: 50% off your first month with code FIRSTMONTHSee slots

Mockito-based unit test patterns for @Service classes that isolate business logic from databases and external APIs.

skills.sh installs
4K
GitHub stars
372
last updated
Aug 21, 2026
View on GitHub

What it does

  • Covers arrange-act-assert tests with mocked repositories, verified method calls, exception scenarios, and stubbed external API responses
  • Includes JUnit 5 examples using @ExtendWith(MockitoExtension.class), @Mock, and @InjectMocks, with AssertJ assertions
  • Enforces isolation by excluding value objects and DTOs from mocking and warning against excessive dependencies or @Spy use
  • Designed for Java teams running mvn test or gradle test who need fast tests without a Spring container or database

Install

$ npx -y skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-service-layer -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Unit Testing Service Layer with Mockito

Overview

Provides patterns for unit testing @Service classes using Mockito. Mocks repository calls, verifies method invocations, tests exception scenarios, and stubs external API responses. Enables fast, isolated tests without Spring container or database.

When to Use

  • Testing business logic in @Service classes
  • Mocking repository and external client dependencies
  • Verifying service interactions with mocked collaborators
  • Testing error handling and edge cases in services
  • Writing fast, isolated unit tests (no database, no API calls)

Instructions

Follow this workflow to test service layer with Mockito, including validation checkpoints:

1. Setup Test Class

Use @ExtendWith(MockitoExtension.class) to enable Mockito annotations.

2. Declare Mocks with @Mock and @InjectMocks

Use @Mock for dependencies (repositories, clients) and @InjectMocks for the service under test.

3. Arrange-Act-Assert with Validation

Arrange: Create test data and configure mock return values using when().thenReturn().

Act: Execute the service method being tested.

Details

Repository
github.com/giuseppe-trisciuoglio/developer-kit
Categories
Testing & QA, Backend & APIs, Java & JVM
First seen
May 16, 2026

More from giuseppe-trisciuoglio/developer-kit

All 95

Similar to Unit Test Service Layer

More Testing & QA