01 / 21

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.

Java

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.

Boundary Value
The min or max of an input range, like \( 0 \), \( -1 \), or \( \text{Integer.MAX_VALUE} \).
Corner Case
A situation where multiple edge conditions combine simultaneously.

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.

Where Bugs Hide Normal Inputs 15% Edge Cases 85% of bugs of bugs Edge cases cover a small input space but reveal the most defects.

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.

\(a-1\)
Invalid
\(a\)
Min valid mid Nominal
\(b\)
Max valid
\(b+1\)
Invalid Valid Range [a, b]

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.

Z
Zero: What happens with \( 0 \), empty strings, empty collections?
O
One: Single element, single character, off-by-one boundaries.
M
Many: Large collections, stress tests, performance limits.
B
Boundaries: \(\text{Integer.MIN_VALUE}\), \(\text{Integer.MAX_VALUE}\), array index limits.
I
Interface: Does it meet the contract? Test pre-/post-conditions.
E
Exceptions: null inputs, negative indices, invalid types.
S
Simple scenarios: The "happy path" baseline that must still pass.

Java's Danger Zones

Java has specific language features that make certain edge cases especially treacherous. Know these by heart.

Java Edge Cases null references Integer overflow Array bounds Empty strings Negative values Division by 0

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

1
Read the method signature. Identify every parameter and its type: int, String, array, object.
2
List valid ranges. For each parameter, write down the minimum and maximum valid values.
3
Apply ZOMBIES. For each parameter, generate Zero, One, Many, Boundary, Interface, Exception, and Simple test cases.
4
Consider type-specific traps. null for objects, \(\text{Integer.MIN_VALUE}\) for ints, empty for collections.
5
Write the tests. Each edge case becomes a separate test method with a clear expected result.

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.

public static int indexOf(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) return i; } return -1; }
Z: Zero
Empty array: indexOf({}, 5) should return \(-1\).
O: One
Single element array, target found and not found.
B: Boundary
Target at first index, target at last index.

indexOf Test Cases

1
Null array: indexOf(null, 5) → should throw NullPointerException or handle gracefully.
2
Empty array: indexOf({}, 5) → expected: \(-1\).
3
Target at index 0: indexOf({5, 3, 7}, 5) → expected: 0.
4
Target at last index: indexOf({3, 7, 5}, 5) → expected: 2.
5
Duplicates: indexOf({5, 5, 5}, 5) → expected: 0 (first occurrence).
6
Integer extreme: indexOf({Integer.MAX_VALUE}, Integer.MAX_VALUE) → expected: 0.

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.

public static String safeSubstring(String s, int start, int end) { if (s == null || start < 0 || end > s.length() || start > end) return ""; return s.substring(start, end); }
null input
s = null → must not throw NPE; return "".
Empty string
s = "", start = 0, end = 0 → return "".
start == end
s = "hello", start = 2, end = 2 → return "".

substring Boundary Map

String s = "hello" (length = 5) 'h' 0 'e' 1 'l' 2 'l' 3 'o' 4 Valid: start = 0, end = 5 → "hello" -1 6 Edge: start = end → "" (empty substring) Invalid: start > end → must handle gracefully Invalid: start < 0 or end > length → guard clause needed

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.

// Buggy version public static int average(int a, int b) { return (a + b) / 2; // Overflows if a + b > Integer.MAX_VALUE! } // Fixed version public static int average(int a, int b) { // This avoids overflow by calculating half the difference return a + (b - a) / 2; }

Overflow Visualized

32-bit Integer Number Line MIN_VALUE -2,147,483,648 0 MAX_VALUE 2,147,483,647 What happens on overflow: MAX_VALUE + 1 = MIN_VALUE (wraps!) Silent: no exception thrown

Common Mistakes

✕ Forgetting null
Not testing null inputs for object parameters. A single missing null check → NullPointerException in production.
✕ Off-By-One Only at End
Testing the upper boundary (e.g., index `length-1` and `length`) but forgetting the lower boundary (e.g., index `0` and `-1`).
✕ Ignoring Empty Inputs
Assuming arrays and strings always have at least one element. Empty collections are valid inputs.
✕ Testing Only Happy Path
Writing tests that pass with "normal" data but never stress the boundaries where bugs actually live.
✕ Assuming No Overflow
Not testing with Integer.MAX_VALUE or Integer.MIN_VALUE. Arithmetic overflow is silent in Java.
✕ No Exception Tests
Not verifying that invalid inputs throw the correct exception type (IllegalArgumentException, etc.).

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)?

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?