November 2024
Intermediate to advanced
300 pages
7h 12m
English
Most verifications are self-explanatory, at least in terms of the code bits they’re trying to verify. Sometimes, it’s helpful to have a bit of “why” or additional context to explain an assertion. “Just why does this test expect the total to be 42?”
Most JUnit assert forms support an optional final argument named message. The message argument allows you to supply a nice verbose explanation of the rationale behind the assertion:
| | @Test |
| | void balanceRepresentsTotalOfDeposits() { |
| | account.deposit(50); |
| | account.deposit(51); |
| | |
| | var balance = account.getBalance(); |
| | |
| | assertEquals(101, balance, "account balance must be total of deposits" ... |
Read now
Unlock full access