CORR[E][C]T: [C]ardinality and [E]xistence
Explore how to apply the CORRECT testing principle to check cardinality and existence in Java unit tests. Understand the importance of testing for null, zero, and varying counts of data to catch defects early and write adaptable tests that handle boundary conditions effectively.
We'll cover the following...
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.
CORRE[C]T: [C]ardinality
As programmers, we usually focus first and most on building the happy path. We generally only consider the unhappy paths as an afterthought when expected data isn’t ...