TE

Java Testing

Java testing conventions

Details

Language / Topic
javaJava
Category
Testing

Rules

balanced
- Follow the Arrange-Act-Assert pattern. One assertion concept per test method.
- Name tests descriptively: `shouldReturnErrorWhenUserNotFound` not `test1`.
- Use Mockito's `@Mock` and `@InjectMocks` annotations with `@ExtendWith(MockitoExtension.class)` — verify interactions with `verify(mock).method()`.
- Use parameterized tests for data-driven scenarios with multiple input/output combinations.
- Use mocking for dependency isolation: mock interfaces, not concrete classes.
- Use nested test classes to group related test scenarios within a test class.
- Test expected exceptions with the appropriate assertion and verify the exception message.