Edge Case Testing
in Java
Learn to systematically identify and test boundary conditions and edge cases that break code: from null references to integer overflow.
What Is an Edge Case?
An edge case is an input or condition that sits at the extreme boundary of what your code is designed to handle. These are the values most likely to reveal hidden bugs: the first element, the last element, empty inputs, and values at the limits of a data type's range.
Why Edge Cases Matter
Most bugs in production don't come from "normal" inputs. They appear when your code encounters something it wasn't expecting. Edge case testing catches bugs before users do.
The Boundary Value Spectrum
For any parameter with a valid range \([a, b]\), systematically test five critical points: just below, at, and just above each boundary, plus a value in the middle.
The ZOMBIES Mnemonic
Use the ZOMBIES acronym to systematically generate edge case tests for any method. Each letter represents a category of inputs you must consider.
Java's Danger Zones
Java has specific language features that make certain edge cases especially treacherous. Know these by heart.
Edge Case vs Normal Testing
| Edge Case Testing | Normal Testing | |
|---|---|---|
| Input Focus | Extremes, boundaries, nulls | Typical, expected values |
| Goal | Find hidden bugs | Verify expected behavior |
| Values Tested | 0, null, MAX_VALUE | 5, "hello", [1,2,3] |
| Bug Discovery | High: catches ~85% of defects | Low: confirms happy path only |
| Effort | Requires systematic thinking | Quick and intuitive |
How to Apply Edge Case Testing
Example 1: Testing indexOf
Consider a method indexOf(int[] arr, int target) that returns the index of the target in the array, or \(-1\) if not found.
indexOf Test Cases
Example 2: Testing substring
Consider a wrapper method: safeSubstring(String s, int start, int end) that returns the substring from index start to end, or an empty string on invalid input.
substring Boundary Map
Example 3: Integer Overflow
A method computes the average of two integers. This innocent-looking code has a critical edge case when both values are large.
Overflow Visualized
Common Mistakes
Edge Case Quick Reference
| Data Type | Edge Cases to Test | Common Bug |
|---|---|---|
| int | 0, -1, 1, MAX_VALUE, MIN_VALUE | Overflow, division by zero |
| String | null, "", " ", single char, very long | NPE, empty check misses " " |
| Array / List | null, empty, 1 element, all same values | IndexOutOfBounds, NPE |
| double | 0.0, -0.0, NaN, Infinity, very small | Precision loss, NaN propagation |
| boolean | true, false (both must be tested) | Only testing one branch |
Question 1 of 5
A method int max(int[] arr) returns the largest element. Which input is the most critical edge case to test?
Question 2 of 5
What does the following code return when called with average(Integer.MAX_VALUE, 1)?
Code:
public static int average(int a, int b) {
return (a + b) / 2;
}
Question 3 of 5
In the ZOMBIES mnemonic, what does the "B" stand for?
Question 4 of 5
A method boolean contains(String haystack, String needle) checks if needle appears in haystack. Which test catches a bug that the others would miss?
Question 5 of 5
For boundary value analysis on a parameter with valid range \([1, 100]\), which set of test values is the most complete?