ICS 121: Assertions
Overview
- What are assertions?
- Assertions in Java
What are assertions?
- Assertions are logical conditions that are tested as the program
runs.
- If the assertion condition fails, an error message is generated
that specifically indicates the fault.
- Assertions implement design
by contract.
- Assertions may be enabled or disabled at run-time
- When to use assertions:
- When you want to clarify the meaning of each operation to
yourself and other developers
- When you want to help catch defects and shorten debugging
time. Remember: error, defect, fault, failure. Assertions produce
failures that are very close to the fault.
- Do not use assertions to check validity of user input. You
always want that checking enabled, so just use regular
if-statements.
Assertions in Java 1.4
- assert condition : optional-error-message;
- E.g., assert i >=0 : "got negative i: " + i;
- Must compile with 'javac -source 1.4' to allow this syntax
- See documentation
- You have a problem on this week's homework for assertions.
example use case templatesample test plan templateProject plan template