Generates Mockito unit tests for Spring @Service classes. Mocks repositories/clients, verifies interactions, tests exceptions/stubs for isolated business logic without DB/APIs.
From developer-kit-javanpx claudepluginhub giuseppe-trisciuoglio/developer-kit --plugin developer-kit-javaThis skill is limited to using the following tools:
references/examples.mdSearches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Enables AI agents to execute x402 payments with per-task budgets, spending controls, and non-custodial wallets via MCP tools. Use when agents pay for APIs, services, or other agents.
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.
@Service classesFollow this workflow to test service layer with Mockito, including validation checkpoints:
Use @ExtendWith(MockitoExtension.class) to enable Mockito annotations.
@Mock and @InjectMocksUse @Mock for dependencies (repositories, clients) and @InjectMocks for the service under test.
Arrange: Create test data and configure mock return values using when().thenReturn().
Act: Execute the service method being tested.
Assert:
verify()Configure mocks to throw exceptions with when().thenThrow().
Validation checkpoint: Verify exception type and message
mvn test or gradle testmvn test jacoco:report@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);
}
}
@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.
@ExtendWith(MockitoExtension.class) for JUnit 5 integrationexpectedUser, actualUser, captor@Spy; partial mocking is harder to understand and maintain.any(), eq()) cannot be mixed with actual values in the same stub.