CORR[E][C]T: [C]ardinality and [E]xistence
Learn the concepts of cardinality and existence in writing unit tests.
CORR[E]CT: [E]
xistence
We can uncover a good number of potential defects by asking ourselves, “Does some given thing exist?” For a given method that accepts an argument or maintains a field, think through what will happen if the value is null, zero, or otherwise empty.
Java libraries tend to choke and throw an exception when faced with non-existent or uninitialized data. Unfortunately, by the time a null value reaches the point where something chokes on it, it can be hard to understand the original source of the problem. An exception that reports a specific message, such as “profile name not set,” greatly simplifies tracking down the problem.
Get hands-on with 1400+ tech skills courses.