| name | spring-tdd-mockito |
| version | 1.0.0 |
| description | TDD (Test-Driven Development) skill with Mockito for Spring Boot.
Guides the Red-Green-Refactor cycle for writing tests first.
|
| triggers | ["write test","tdd","mockito","unit test","test first"] |
Spring Boot TDD with Mockito
TDD Cycle: Red โ Green โ Refactor
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ TDD WORKFLOW โ
โ โ
โ โโโโโโโโโ โโโโโโโโโ โโโโโโโโโโโโ โ
โ โ RED โ โโโโ โ GREEN โ โโโโ โ REFACTOR โ โโโโ โ
โ โ โ โ โ โ โ โ โ
โ โ Write โ โ Write โ โ Improve โ โ โ
โ โ Test โ โ Code โ โ Code โ โ โ
โ โโโโโโโโโ โโโโโโโโโ โโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Mockito Annotations
| Annotation | Purpose |
|---|
@Mock | Create mock object |
@InjectMocks | Inject mocks into class under test |
@Spy | Partial mock - real methods + mock behavior |
@Captor | Capture arguments passed to mock |
@MockBean | Mock bean in Spring context |
Test Template
@ExtendWith(MockitoExtension.class)
class ServiceTest {
@Mock
private Repository repository;
@InjectMocks
private Service service;
@Test
@DisplayName("Should do something when condition is met")
void shouldDoSomethingWhenConditionIsMet() {
when(repository.findById(1L)).thenReturn(Optional.of(entity));
var result = service.doSomething(1L);
assertThat(result).isNotNull();
verify(repository).findById(1L);
}
}
Best Practices
- One assertion per test - Focus on single behavior
- Descriptive test names - Use
@DisplayName
- AAA Pattern - Arrange, Act, Assert
- Test behavior, not implementation
- Fast tests - Mock external dependencies
- Isolated tests - No shared state
origin: https://github.com/majiayu000/claude-skill-registry/blob/main/skills/data/spring-tdd-mockito/SKILL.md