|
| 1 | +# Testing Conventions |
| 2 | + |
| 3 | +This document outlines the testing conventions used in our project. |
| 4 | + |
| 5 | +## Test Class Naming |
| 6 | + |
| 7 | +- **Pattern**: `{ClassName}Test` |
| 8 | +- **Examples**: |
| 9 | + - `ExecutionController` → `ExecutionControllerTest` |
| 10 | + - `FormService` → `FormServiceTest` |
| 11 | + |
| 12 | +## Test Method Naming |
| 13 | + |
| 14 | +### For Service Methods |
| 15 | +- **Pattern**: `{methodName}{condition}{expected}` |
| 16 | +- **Examples**: |
| 17 | + - `findUserByIdWhenUserExistsReturnsUser` |
| 18 | + - `saveUserWhenEmailIsInvalidThrowsValidationException` |
| 19 | + |
| 20 | +## Test Structure (Given-When-Then) |
| 21 | + |
| 22 | +Always structure tests using the Given-When-Then pattern: |
| 23 | + |
| 24 | +### Basic Structure |
| 25 | +```java |
| 26 | +@Test |
| 27 | +@DisplayName("Descriptive test name only if method is too complex") |
| 28 | +void methodName_condition_expected() { |
| 29 | + // Given - setup test data and preconditions |
| 30 | + |
| 31 | + // When - perform the action being tested |
| 32 | + |
| 33 | + // Then - verify the results |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### Example: Service Test (GWT parts are delimited by newline) |
| 38 | +```java |
| 39 | +@Test |
| 40 | +void findUserByIdWhenUserExistsReturnsUser() { |
| 41 | + User expectedUser = new User("1", "test@example.com"); |
| 42 | + when(userRepository.findById("1")).thenReturn(Optional.of(expectedUser)); |
| 43 | + |
| 44 | + User result = userService.findUserById("1"); |
| 45 | + |
| 46 | + assertThat(result).isEqualTo(expectedUser); |
| 47 | + verify(userRepository).findById("1"); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Example: One-liner that is too simple to split into GWT parts |
| 52 | +```java |
| 53 | +@Test |
| 54 | +void findById_returnsEmpty_whenNotFound() { |
| 55 | + assertThat(repository.findById("nonexistent")).isEmpty(); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Best Practices |
| 60 | + |
| 61 | +1. **One Assertion Per Test**: Each test should verify one behavior |
| 62 | +2. **Descriptive Names**: Test names should clearly describe the scenario |
| 63 | +3. **Test Isolation**: Each test should be independent of others |
| 64 | +4. **Readability**: Use empty lines to separate GWT sections for better readability |
| 65 | +5. **Use @DisplayName**: For more descriptive test output only if it is hard to understand from the method name |
| 66 | +6. **Nested Tests**: Use `@Nested` to group related test cases |
0 commit comments