
Unit Test Service Layer
This gives you the Mockito and JUnit 5 boilerplate for testing Spring service classes without spinning up a container or hitting a database. It covers the standard patterns: mocking repositories with @Mock and @InjectMocks, stubbing return values with when().thenReturn(), verifying interactions, and testing exception paths. The arrange-act-assert structure with validation checkpoints is practical, and the warnings about not mocking DTOs or mixing argument matchers are things people actually get wrong. Use this when you need to test business logic in isolation and want fast feedback loops. The examples are straightforward repository and email service scenarios that map to real codebases.
- skills.sh installs
- 3.9K
- GitHub stars
- 369
- last updated
- Aug 21, 2026
Install
$ npx -y skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-service-layer --agent claude-codeInstalls into .claude/skills of the current project.
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
@Serviceclasses - 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.
Sponsors
Details
- Repository
- github.com/giuseppe-trisciuoglio/developer-kit
- Categories
- Backend & APIs, Testing & QA, Debugging, Databases
- First seen
- May 16, 2026
More from giuseppe-trisciuoglio/developer-kit
All 102- Nextjs Code Review3.9K
- Spring Ai Mcp Server Patterns3.9K
- Unit Test Wiremock Rest Api3.9K
- Spring Boot Actuator3.9K
- Nextjs Authentication3.9K
- Spring Boot Dependency Injection3.9K
- Unit Test Controller Layer3.9K
- Unit Test Parameterized3.9K
- Unit Test Json Serialization3.9K
- Spring Boot Saga Pattern3.9K
- Spring Data Neo4j3.9K
- Unit Test Security Authorization3.9K
- Nextjs Data Fetching3.9K
- Langchain4j Tool Function Calling Patterns3.9K
- Nx Monorepo3.9K
- Langchain4j Spring Boot Integration3.9K
- Langchain4j Rag Implementation Patterns3.9K
- Unit Test Scheduled Async3.9K
- Rag3.9K
- Unit Test Utility Methods3.9K
- Qdrant3.9K
- Unit Test Exception Handler3.9K
- Unit Test Mapper Converter3.9K
- Langchain4j Ai Services Patterns3.9K