unit-test-service-layer
Provides patterns for unit testing service layer with Mockito. Creates isolated tests that mock repository calls, verify method invocations, test exception scenarios, and stub external API responses
Install / Use
npx skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-service-layerInstalls into whichever agent you are using.
SKILL.md
Installable skill definition
Quality Score
Category
Data & AnalyticsSupported Platforms
Our assessment of unit-test-service-layer
unit-test-service-layer scores 83/100 on our quality scale, 450th of 589 Data & Analytics skills we index.
Its SKILL.md is 4.6 KB long, well organised into 15 sections with 2 code examples: a solid amount of guidance for an agent.
It has 353 GitHub stars, a meaningful sign that others use it.
Maintenance, license and trust
- The repository was last updated 26 days ago, so unit-test-service-layer is actively maintained.
- It is released under the MIT license, a permissive license that allows use, modification and commercial use with attribution.
- Its trust signals score 100/100, with no cautions. These come from repository metadata, not a code audit — read the skill file before letting an agent act on it.
unit-test-service-layer compared with similar skills
All 4 of these similar skills score higher than unit-test-service-layer; compare them before choosing.
| Skill | Score | Stars | Updated | Format |
|---|---|---|---|---|
| unit-test-service-layer (this skill)by giuseppe-trisciuoglio | 83 | 353 | 26d ago | SKILL.md |
| Agent-Reachby Panniantong | 100 | 92.6k | 21d ago | CLAUDE.md |
| headroomby headroomlabs-ai | 100 | 74.5k | today | CLAUDE.md |
| Scraplingby D4Vinci | 100 | 86.0k | today | MCP Server |
| crawl4aiby unclecode | 100 | 84.8k | 1d ago | MCP Server |
Frequently asked questions
- How do I install unit-test-service-layer?
- Run
npx skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-service-layer. The install tabs above show the steps for each supported agent. - Which AI agents does unit-test-service-layer work with?
- It is written for Universal, as a SKILL.md file. Other agents that read the same format can often use it too.
- Is unit-test-service-layer safe to use?
- It is MIT-licensed and scores 100/100 on trust signals. Skills are instructions an agent will follow, so read the file before installing it and do not approve commands you do not understand.
- Is unit-test-service-layer still maintained?
- The repository was last updated 26 days ago, so unit-test-service-layer is actively maintained.
Skill content
View source on GitHubname: unit-test-service-layer description: Provides patterns for unit testing service layer with Mockito. Creates isolated tests that mock repository calls, verify method invocations, test exception scenarios, and stub external API responses. Use when testing service behaviors and business logic without database or external services. allowed-tools: Read, Write, Bash, Glob, Grep
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.
Assert:
- Verify returned values with AssertJ assertions
- Verify mock interactions with
verify() - Validation checkpoint: Run test and confirm green bar
4. Test Exception Scenarios
Configure mocks to throw exceptions with when().thenThrow().
Validation checkpoint: Verify exception type and message
5. Verify Complete Coverage
- Run full test suite:
mvn testorgradle test - Check coverage report:
mvn test jacoco:report - Validation checkpoint: Confirm all service methods have corresponding tests
Examples
Basic Service Test Pattern
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
void shouldReturnUserWhenFound() {
// Arrange
User expected = new User(1L, "Alice");
when(userRepository.findById(1L)).thenReturn(Optional.of(expected));
// Act
User result = userService.getUser(1L);
// Assert
assertThat(result.getName()).isEqualTo("Alice");
verify(userRepository).findById(1L);
}
@Test
void shouldThrowWhenUserNotFound() {
// Arrange
when(userRepository.findById(999L)).thenReturn(Optional.empty());
// Act & Assert
assertThatThrownBy(() -> userService.getUser(999L))
.isInstanceOf(UserNotFoundException.class);
}
}
Verify Method Invocations
@Test
void shouldSendEmailOnUserCreation() {
User newUser = new User(1L, "Alice", "alice@example.com");
when(userRepository.save(any(User.class))).thenReturn(newUser);
enrichmentService.registerNewUser("Alice", "alice@example.com");
verify(userRepository).save(any(User.class));
verify(emailService).sendWelcomeEmail("alice@example.com");
}
For additional patterns (multiple dependencies, argument captors, async services, InOrder verification), see references/examples.md.
Best Practices
- Use
@ExtendWith(MockitoExtension.class)for JUnit 5 integration - Mock only direct dependencies of the service under test
- Verify interactions to ensure correct collaboration
- Test one behavior per test method - keep tests focused
- Use descriptive variable names:
expectedUser,actualUser,captor - Create real instances for value objects and DTOs (don't mock them)
Constraints and Warnings
- Do not mock value objects or DTOs; create real instances with test data.
- Avoid mocking too many dependencies; consider refactoring if a service has too many collaborators.
- Tests must be independent; do not rely on execution order.
- Be cautious with
@Spy; partial mocking is harder to understand and maintain. - Do not test private methods directly; test them through public method behavior.
- Argument matchers (
any(),eq()) cannot be mixed with actual values in the same stub. - Avoid over-verifying; verify only interactions important to the test scenario.
References
Related Skills
Agent-Reach
92.6kGive your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.
headroom
74.5kCompress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Scrapling
86.0k🕷️ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev
crawl4ai
84.8kOpen-source web crawler and scraper for LLMs and AI agents: any website into clean, LLM-ready Markdown. Run it yourself, or use Crawl4AI Cloud with one key.
Languages
Trust signals
From repository metadata: license, adoption, age and documentation. Not a code audit — see the Safety scan above for what the skill file itself contains.
