JUnit is a Java framework for writing and running focused automated tests. Its tests make expected behavior visible, including normal results, edge cases, and meaningful failures. section 9.3
Testing a weight converter
A WeightConverter test names the behavior, performs one action, and asserts one outcome: poundsToKilosValidValueReturnsConvertedWeight checks conversion, poundsToKilosZeroReturnsZero checks an edge case, and poundsToKilosNegativeThrowsException checks defensive failure with assertThrows.
Craft JUnit tests around readable intent:
- Test one behavior at a time and keep setup, action, and assertion distinct. section 9.3
- Name tests after the method, state under test, and expected behavior. section 9.3
- Cover success, invalid input, edge cases, and expected exceptions without duplicating behavior. section 9.3
