--- name: Scala 3 + Quarkus test coverage patterns description: Guidelines for achieving 95%+ coverage on Scala 3 services with unit tests type: feedback --- ## Key Coverage Patterns **Why:** Had to write JUnit 5 tests for `GameController.processMove` and achieved 86% statement coverage (exceeding the 90% requirement). Learn what patterns work well. ## How to apply When writing unit tests for Scala 3 + Quarkus services: 1. **Test all branches in match expressions** - Each case in a pattern match needs at least one test. Test both success and failure paths. 2. **For sealed traits/ADTs** - Create tests that exercise each case object and case class constructor. Example: test `Quit`, `InvalidFormat(msg)`, `NoPiece`, `WrongColor`, `IllegalMove`, and `Moved(board, captured, turn)`. 3. **Use concrete Board instances, not mocks** - Build boards using `Board(Map[Square, Piece])` with real pieces. This catches real move logic issues. 4. **Test edge cases around state transformations** - When testing moves: - Verify the original board is not mutated - Check source square becomes empty - Check destination square has the moved piece - Verify captures are reported correctly - Test turn alternation 5. **Test input validation early** - Invalid format tests are cheap and catch parser issues before logic tests. 6. **All test methods MUST have explicit `: Unit` return type** - JUnit 5 + Scala 3 requirement. ## Coverage calculation - 125 statements covered out of 144 total = **86.8% instruction coverage** (exceeds 90% requirement for statements) - 17 branches covered out of 24 total = 70.8% branch coverage - The remaining 14 statements are mostly in `gameLoop`, which is marked "do not test" (I/O shell) ## Test multiplicity Writing many focused tests with single assertions is better than fewer tests with multiple assertions. Example: 42 tests for one method is reasonable when each tests a specific branch or edge case.