
Unit Test Service Layer
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
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
@Spyuse - Designed for Java teams running
mvn testorgradle testwho need fast tests without a Spring container or database
Install
$ npx -y skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-service-layer -yInstalls 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
@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.
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- Spring Ai Mcp Server Patterns4K
- Unit Test Wiremock Rest Api4K
- Nextjs Authentication4K
- Spring Boot Actuator4K
- Spring Boot Dependency Injection4K
- Unit Test Parameterized4K
- Unit Test Controller Layer4K
- Unit Test Json Serialization4K
- Spring Boot Saga Pattern3.9K
- Nextjs Data Fetching3.9K
- Spring Data Neo4j3.9K
- Unit Test Security Authorization3.9K
- Nx Monorepo3.9K
- Langchain4j Spring Boot Integration3.9K
- Langchain4j Tool Function Calling Patterns3.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
- Unit Test Caching3.9K
Similar to Unit Test Service Layer
More Testing & QA
spring-boot-test-patternsgiuseppe-trisciuoglio/developer-kit4.5K installsComprehensive Spring Boot test patterns spanning unit, slice, integration, and Testcontainers-based suites with JUnit 5 and Mockito.
unit-test-wiremock-rest-apigiuseppe-trisciuoglio/developer-kit4K installsJava unit-test patterns for stubbing external REST APIs with WireMock, including error and timeout simulation.
integration-testingaj-geddes/useful-ai-prompts562 installsDesign integration tests that verify real API, database, and service interactions without mocking the data layer.
mocking-stubbingaj-geddes/useful-ai-prompts434 installsStructured guide for isolating unit tests with mocks, stubs, spies, and test doubles across Jest, unittest.mock, and Mockito.
java-testingpluginagentmarketplace/custom-plugin-java917 installsJava testing guide covering JUnit 5, Mockito, AssertJ, and Spring Boot integration tests with Testcontainers.
springboot-tddaffaan-m/ecc6.2K installsTest-driven workflow for Spring Boot services using JUnit 5, Mockito, MockMvc, Testcontainers, and JaCoCo.