unit-test-service-layer
Master isolated service layer testing using Mockito to mock repositories and external clients without database or API calls. This skill teaches you to arrange test data, verify method interactions, and handle exception scenarios with AssertJ assertions.
Unit Test Service Layer teaches you to write isolated Mockito-based tests for service classes with mocked dependencies and verified interactions.
AI-generated summary based on this skill's SKILL.md
Install
giuseppe-trisciuoglio/developer-kit/unit-test-service-layer · repository language: Python
git clone https://github.com/giuseppe-trisciuoglio/developer-kit
cp -r developer-kit/plugins/developer-kit-java/skills/unit-test-service-layer ~/.claude/skills/unit-test-service-layernpx skillfed install giuseppe-trisciuoglio/developer-kit/unit-test-service-layerFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How to unit test service layer with Mockito?
unit-test-service-layer teaches you to isolate service classes by mocking their dependencies—repositories, external clients, and collaborators. Use @Mock to create mock objects, @InjectMocks to inject them into your service under test, and @ExtendWith(MockitoExtension.class) to enable Mockito annotations. Arrange test data with when/thenReturn, execute the service method, then assert outcomes with AssertJ. This approach eliminates database and API calls, keeping tests fast and focused on business logic.
What patterns does unit-test-service-layer cover for mocking repositories?
unit-test-service-layer demonstrates how to mock repository and external dependency calls to simulate various scenarios. Use Mockito's when().thenReturn() to stub successful responses, when().thenThrow() to simulate exceptions, and verify() to confirm your service called collaborators correctly. Mock repository methods return test data directly, letting you test service logic in complete isolation without touching a real database.
How do I test service business logic and error scenarios in isolation?
unit-test-service-layer covers arranging test conditions with mocked dependencies, executing service methods under controlled conditions, and asserting both happy-path and exception-handling outcomes. Mock external calls to return errors or throw exceptions, then verify your service handles them properly. This isolation ensures you test only the service's own logic, not infrastructure or third-party behavior.
Can unit-test-service-layer help verify service method interactions?
Yes. unit-test-service-layer teaches you to use Mockito's verify() to confirm your service called collaborators with the right arguments, the correct number of times, and in the expected order. Combine verify() with argument matchers to assert precise interaction patterns. This ensures your service orchestrates its dependencies correctly without relying on actual implementations.
Why use unit-test-service-layer for fast, isolated service tests?
unit-test-service-layer eliminates database connections, API calls, and external service dependencies by replacing them with Mockito mocks. Tests run in milliseconds instead of seconds, execute reliably without network or infrastructure, and remain deterministic. This speed and isolation make your test suite maintainable and suitable for continuous integration pipelines.
What is the @Mock and @InjectMocks setup in unit-test-service-layer?
unit-test-service-layer uses @Mock to declare dependencies you want to mock, and @InjectMocks to automatically inject those mocks into your service class. Pair this with @ExtendWith(MockitoExtension.class) on your test class. This setup eliminates manual mock creation and wiring, letting you focus on arranging behavior with when/thenReturn and verifying interactions with verify().
SKILL.md
rendered from the published skill — quoted content, verbatim
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
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 2 files
plugins/developer-kit-java/skills/unit-test-service-layer/SKILL.md
plugins/developer-kit-java/skills/unit-test-service-layer/references/examples.md