When to use
Use this skill when creating or modifying tests in this project.
Core principles
- Use AAA structure in every test.
- Keep Act phase to one line in unit tests.
- Keep tests deterministic (no hidden time/env/network dependencies).
- Use descriptive method names that state scenario and expected behavior.
- Pick the narrowest useful test type:
- unit tests for pure business logic,
- slice tests for web/repository concerns,
- full integration only when cross-layer wiring matters.
Testing stack by layer
- Unit: JUnit 5 + Mockito.
- Controller slice:
@WebMvcTest+MockMvc. - Persistence/integration:
@SpringBootTestor@DataJpaTestwith in-memory DB. - Optional: Testcontainers for infrastructure fidelity when in-memory DB is insufficient.
Persistence tests
Applies to repository/integration-style tests.
- Use transactional rollback per test class (
@Transactional+@Rollback) to avoid side effects. - Seed stable data via SQL scripts when needed (
src/test/resources/sql/+@Sql). - After write operations (
create/update/delete), callEntityManager.flush()before assertions. - Verify DB state independently from ORM behavior whenever possible:
JdbcTestUtils.countRowsInTableJdbcTestUtils.countRowsInTableWhere
- Keep shared constants and entity factories in a central
InstanceProvider. - Prefer fixed IDs/emails/names in test constants that match SQL seeds.
- File naming:
<Entity>RepositoryTest.
Persistence example
@Transactional
@Rollback
@SpringBootTest
class UserRepositoryTest {
private static final String EMAIL = "test@example.com";
@PersistenceContext
private EntityManager em;
@Autowired
private UserRepository userRepository;
@Autowired
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
@BeforeEach
void setUp() {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Test
void testCreate() {
// Arrange
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "users");
// Act
User user = userRepository.create(EMAIL, "secret", "John", "Doe");
em.flush();
// Assert
assertEquals(before + 1, JdbcTestUtils.countRowsInTable(jdbcTemplate, "users"));
assertNotNull(user);
}
}
Service tests
Pure unit tests (no Spring context).
- Use Mockito JUnit integration (
@ExtendWith(MockitoExtension.class)). - Mock collaborators with
@Mock. - Build subject with
@InjectMocks. - Stub only what the scenario needs.
- Use
assertThrowsfor failure scenarios. - File naming:
<Service>ImplTest.
Service example
@ExtendWith(MockitoExtension.class)
class UserServiceImplTest {
private static final long USER_ID = 1L;
@InjectMocks
private UserServiceImpl userService;
@Mock
private UserRepository userRepository;
@Test
void testFindByIdExistingUser() {
// Arrange
when(userRepository.findById(USER_ID)).thenReturn(Optional.of(new User()));
// Act
Optional<User> result = userService.findById(USER_ID);
// Assert
assertTrue(result.isPresent());
}
@Test
void testFindByIdNonExisting() {
// Arrange
when(userRepository.findById(anyLong())).thenReturn(Optional.empty());
// Act
Optional<User> result = userService.findById(1L);
// Assert
assertFalse(result.isPresent());
}
}
Controller tests
Use Mockito helpers or slice tests based on scope.
- For request logic/helpers, prefer Mockito unit tests.
- For endpoint contract and MVC behavior, use
@WebMvcTest+MockMvc. - Mock auth/request objects as needed.
- If entity IDs are generated and lack setters, use local test helpers (reflection-based if required).
- File naming:
<Entity>ControllerTest.
Controller helper pattern
private User createUserWithId(String email, Long id) throws Exception {
User user = new User(email);
Field idField = User.class.getDeclaredField("id");
idField.setAccessible(true);
idField.set(user, id);
return user;
}
Keep small helper methods (createUserWithId, createMockAuthentication, etc.) inside the test class to reduce
duplication and improve readability.
Test review checklist
- AAA shape is explicit.
- Act phase is one line (unit tests).
- Assertions validate outcome and important side effects.
- Collaborator interactions are verified only when behaviorally relevant.
- Test names and fixtures clearly express intent.
- Repository tests flush before asserting persisted state.
- Controller tests cover validation/error paths for request boundaries.