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-code

Installs 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 @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.

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
1giuseppe-trisciuoglio avatarnextjs-code-reviewgiuseppe-trisciuoglio/developer-kitProvides comprehensive code review capability for Next.js applications, validates Server Components, Client Components, Server Actions, caching strategies, meta3.9Kskills.sh installs
2yaklang avatarkubernetes-pentestingyaklang/hack-skillsKubernetes penetration testing playbook. Use when targeting Kubernetes clusters via API server, RBAC enumeration, service account abuse, etcd access, Kubelet AP3.9Kskills.sh installs
3inngest avatarinngest-setupinngest/inngest-skillsUse when adding durable execution to a TypeScript project — building retry-safe webhook handlers, background jobs that survive crashes, scheduled tasks, or long3.9Kskills.sh installs
4mongodb avatarmongodb-atlas-stream-processingmongodb/agent-skillsManages MongoDB Atlas Stream Processing (ASP) workflows. Handles workspace provisioning, data source/sink connections, processor lifecycle operations, debugging3.9Kskills.sh installs
5giuseppe-trisciuoglio avatarunit-test-controller-layergiuseppe-trisciuoglio/developer-kitProvides patterns for unit testing REST controllers using MockMvc and @WebMvcTest. Generates controller tests that validates request/response mapping, validatio3.9Kskills.sh installs
6giuseppe-trisciuoglio avatarunit-test-exception-handlergiuseppe-trisciuoglio/developer-kitProvides patterns for unit testing `@ExceptionHandler` and `@ControllerAdvice` in Spring Boot applications. Validates error response formatting, mocks exception3.9Kskills.sh installs