unit-test-service-layer

Solid

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.

Testing & QA 261 stars 29 forks Updated 1 weeks ago MIT

Install

View on GitHub

Quality Score: 89/100

Stars 20%
81
Recency 20%
90
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# 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. **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 Cov...

Details

Author
giuseppe-trisciuoglio
Repository
giuseppe-trisciuoglio/developer-kit
Created
7 months ago
Last Updated
1 weeks ago
Language
Python
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

Testing & QA Listed

testing-patterns

Backend testing patterns with JUnit 6, Mockito 6, Testcontainers 2.0, Spring Boot slice tests, RestTestClient, and security testing. Use when user mentions testing, coverage, TDD, integration tests, or "write tests for".

0 Updated today
IuliaIvanaPatras
Testing & QA Solid

unit-test-controller-layer

Provides patterns for unit testing REST controllers using MockMvc and @WebMvcTest. Generates controller tests that validates request/response mapping, validation, exception handling, and HTTP status codes. Use when testing web layer endpoints in isolation for API endpoint testing, Spring MVC tests, mock HTTP requests, or controller layer unit tests.

261 Updated 1 weeks ago
giuseppe-trisciuoglio
Testing & QA Listed

mockito-expert

Use when mocking, stubbing, spying, or verifying collaborator behavior in Java unit tests. Invoke for mock creation, argument matchers, stubbing strategies, verification, argument captors, answer implementations, strict stubbing, spy objects, and Mockito extensions with JUnit 5.

0 Updated 1 weeks ago
Ortus-Solutions
Testing & QA Solid

spring-boot-test-patterns

Provides comprehensive testing patterns for Spring Boot applications covering unit, integration, slice, and container-based testing with JUnit 5, Mockito, Testcontainers, and performance optimization. Use when writing tests, @Test methods, @MockBean mocks, or implementing test suites for Spring Boot applications.

261 Updated 1 weeks ago
giuseppe-trisciuoglio
Testing & QA Listed

oracle-testing

Stub-Driven TDD and layer boundary testing. Use when writing tests, deciding what to test, or testing at component boundaries.

28 Updated 1 weeks ago
martinffx