Testing edge cases and boundaries
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)ShopLite charged delivery on exactly one order value, and no test noticed. You cannot test every input, so the skill is to choose the few inputs most likely to break the code.
Equivalence partitions: one test per class of input
An equivalence partition is a group of inputs the code should treat the same way. Testing one value from each group covers the whole group.
A fare depends on age: under 5 free, 5 to 11 half, 12 to 59 full, 60 and above senior, negative invalid. Test one representative of each group.
Example 1 · One representative per partition
class TicketFare { // Full fare Rs 400. Under 5: free. 5 to 11: half. 12 to 59: full. 60 and above: 40% off. static int fare(int age) { if (age < 0) throw new IllegalArgumentException("Age cannot be negative: " + age); if (age < 5) return 0; if (age < 12) return 200; if (age < 60) return 400; return 240; }}
public class FarePartitions { static void check(String name, int expected, int actual) { System.out.println((expected == actual ? "PASS " : "FAIL ") + name + (expected == actual ? "" : ": expected " + expected + " but was " + actual)); }
public static void main(String[] args) { check("under 5 travels free (age 3)", 0, TicketFare.fare(3)); check("5 to 11 pay half (age 8)", 200, TicketFare.fare(8)); check("12 to 59 pay full fare (age 30)", 400, TicketFare.fare(30)); check("60 and above get 40% off (age 70)", 240, TicketFare.fare(70)); try { TicketFare.fare(-1); System.out.println("FAIL negative age is rejected"); } catch (IllegalArgumentException e) { System.out.println("PASS negative age is rejected: " + e.getMessage()); } }}PASS under 5 travels free (age 3) PASS 5 to 11 pay half (age 8) PASS 12 to 59 pay full fare (age 30) PASS 60 and above get 40% off (age 70) PASS negative age is rejected: Age cannot be negative: -1
What this code does
- Five tests cover five partitions, including the invalid one. Every other age is in one of these groups, so another middle value such as 31 would test nothing new.
- These tests prove each rule exists; they do not yet prove where each rule starts. That is the job of boundary tests.
Testing age 30 and age 31 adds nothing: both are in the same partition. One representative per partition gives the widest cover for the fewest tests, and an invalid partition is a partition too.
Boundary values: just below, at and just above
Boundary value analysis means testing each edge between two partitions from both sides: the last value of one group and the first value of the next, such as 59 and 60.
Most bugs live at the edges, because < and <= look alike. The table below tests every edge from both sides.
Example 2 · Boundary values on both sides of each edge
class TicketFare { // Full fare Rs 400. Under 5: free. 5 to 11: half. 12 to 59: full. 60 and above: 40% off. static int fare(int age) { if (age < 0) throw new IllegalArgumentException("Age cannot be negative: " + age); if (age < 5) return 0; if (age < 12) return 200; if (age < 60) return 400; return 240; }}
public class FareBoundaries { static void check(String name, int expected, int actual) { System.out.println((expected == actual ? "PASS " : "FAIL ") + name + (expected == actual ? "" : ": expected " + expected + " but was " + actual)); }
public static void main(String[] args) { int[][] boundaries = { {0, 0}, {4, 0}, {5, 200}, {11, 200}, {12, 400}, {59, 400}, {60, 240}, {120, 240}, }; for (int[] b : boundaries) { check("age " + b[0], b[1], TicketFare.fare(b[0])); } }}PASS age 0 PASS age 4 PASS age 5 PASS age 11 PASS age 12 PASS age 59 PASS age 60 PASS age 120
What this code does
- Each edge is tested from both sides: 4 and 5, 11 and 12, 59 and 60. Age 0 is the lowest valid value and 120 a sensible top, both edges of the whole range.
- Written as a table, the boundaries are easy to review against the rule "under 5 free, 5 to 11 half, 12 to 59 full, 60 and above 40% off".
A boundary is the edge between two partitions, such as the 60 between full fare and senior fare. For whole numbers, "just below" and "at" are one apart. For money they are one paisa apart: Rs 998.99, 999.00 and 999.01 around a Rs 999 limit.
Empty, null, zero, negative and very large inputs
An edge input is a value that breaks code in almost every project: empty, null, zero, negative, very large, or text that is not a number.
Some inputs belong on every test list: empty, null, zero, negative, very large and not a number. The table below decides the answer for each.
Example 3 · Empty, null, zero, negative and too large
class QuantityParser { // Reads the quantity a customer typed in the cart: a whole number from 1 to 10 static int parse(String text) { if (text == null || text.isBlank()) { throw new IllegalArgumentException("Quantity is required"); } int quantity; try { quantity = Integer.parseInt(text.trim()); } catch (NumberFormatException e) { throw new IllegalArgumentException("Quantity must be a whole number: " + text); } if (quantity < 1 || quantity > 10) { throw new IllegalArgumentException("Quantity must be 1 to 10: " + quantity); } return quantity; }}
public class EdgeInputs { public static void main(String[] args) { String[] inputs = {null, "", " ", "0", "-2", "abc", "2.5", "99999999999", " 3 ", "10", "11"}; for (String input : inputs) { String shown = input == null ? "null" : "\"" + input + "\""; try { System.out.println(shown + " -> " + QuantityParser.parse(input)); } catch (IllegalArgumentException e) { System.out.println(shown + " -> rejected: " + e.getMessage()); } } }}null -> rejected: Quantity is required "" -> rejected: Quantity is required " " -> rejected: Quantity is required "0" -> rejected: Quantity must be 1 to 10: 0 "-2" -> rejected: Quantity must be 1 to 10: -2 "abc" -> rejected: Quantity must be a whole number: abc "2.5" -> rejected: Quantity must be a whole number: 2.5 "99999999999" -> rejected: Quantity must be a whole number: 99999999999 " 3 " -> 3 "10" -> 10 "11" -> rejected: Quantity must be 1 to 10: 11
What this code does
- Every classic edge input has a clear, planned answer. Without the null check,
nullwould crash with aNullPointerException; without thetry,99999999999(too big for anint) would leak a rawNumberFormatExceptionto the user. - The table also covers the boundaries of the valid range (
10accepted,11and0rejected) and a harmless edge, spaces around a valid number, which is trimmed and accepted on purpose.
For each one, decide the expected behaviour first, often a clear exception with a helpful message, and write it as a test. An unclear crash such as a NullPointerException deep inside the code is exactly the bug these tests find.
Money rounding with BigDecimal and RoundingMode.HALF_UP
HALF_UP is the rounding mode that rounds a value ending exactly in half upwards, so 0.005 becomes 0.01. It is the rule most people expect on bills; BigDecimal applies it with RoundingMode.HALF_UP.
Money has its own edge cases: values ending in half a paisa, and splits that do not divide evenly. This example tests both.
Example 4 · Money edges: half a paisa and splits that must add up
import java.math.BigDecimal;import java.math.RoundingMode;import java.util.ArrayList;import java.util.List;
public class MoneyEdges { static BigDecimal gst18(BigDecimal amount) { return amount.multiply(new BigDecimal("0.18")).setScale(2, RoundingMode.HALF_UP); }
// Split a bill so the parts add up exactly: the first people pay the extra paise static List<BigDecimal> split(BigDecimal total, int people) { BigDecimal share = total.divide(BigDecimal.valueOf(people), 2, RoundingMode.DOWN); BigDecimal remainder = total.subtract(share.multiply(BigDecimal.valueOf(people))); int extraPaise = remainder.movePointRight(2).intValueExact(); List<BigDecimal> parts = new ArrayList<>(); for (int i = 0; i < people; i++) { parts.add(i < extraPaise ? share.add(new BigDecimal("0.01")) : share); } return parts; }
public static void main(String[] args) { System.out.println("GST on 12.25 -> " + gst18(new BigDecimal("12.25")) + " (2.205 rounds half up)"); System.out.println("GST on 999.99 -> " + gst18(new BigDecimal("999.99"))); System.out.println("12.345 HALF_UP -> " + new BigDecimal("12.345").setScale(2, RoundingMode.HALF_UP)); System.out.println("12.345 HALF_EVEN -> " + new BigDecimal("12.345").setScale(2, RoundingMode.HALF_EVEN));
List<BigDecimal> parts = split(new BigDecimal("100.00"), 3); BigDecimal sum = parts.stream().reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println("100.00 / 3 -> " + parts + ", sum " + sum);
BigDecimal naive = new BigDecimal("100.00").divide(BigDecimal.valueOf(3), 2, RoundingMode.HALF_UP); System.out.println("naive 3 x " + naive + " = " + naive.multiply(BigDecimal.valueOf(3))); }}GST on 12.25 -> 2.21 (2.205 rounds half up) GST on 999.99 -> 180.00 12.345 HALF_UP -> 12.35 12.345 HALF_EVEN -> 12.34 100.00 / 3 -> [33.34, 33.33, 33.33], sum 100.00 naive 3 x 33.33 = 99.99
What this code does
12.25 × 0.18is exactly2.2050, a half-paisa case: HALF_UP gives 2.21. The12.345lines show why the rounding mode must be a deliberate, tested choice: HALF_EVEN (banker's rounding) gives 12.34.- The naive split loses a paisa (3 × 33.33 = 99.99). The test "parts add up to the total" is the edge case that catches it, and the fixed
splitgives the extra paisa to the first person.
Test the half cases directly (12.345 → 12.35), and test that the parts add up to the whole: splitting Rs 100.00 between 3 friends must give 33.34 + 33.33 + 33.33, not three times 33.33 with a paisa lost.
Predict the output
Predict the money result
Read the code and choose what it prints.
Round 1 of 2
A value that ends exactly in half a paisa.
double cannot hold most paise exactly, so rounding a double can go the "wrong" way at .5. Keep money in BigDecimal from the first test.Off-by-one bugs caught by boundary tests
An off-by-one bug is a mistake by exactly one step at an edge, such as > instead of >= or a loop that runs one time too many. Only a test at the boundary catches it.
Tests with middle values pass happily; only a test at the boundary fails. First find the line that moves the edge by one, then see the bug caught.
Example 5 · An off-by-one bug only the boundary test catches
class BuggyFare { static int fare(int age) { if (age < 5) return 0; if (age < 12) return 200; if (age <= 60) return 400; // bug: should be age < 60 return 240; }}
public class OffByOne { static void check(String name, int expected, int actual) { System.out.println((expected == actual ? "PASS " : "FAIL ") + name + (expected == actual ? "" : ": expected " + expected + " but was " + actual)); }
public static void main(String[] args) { System.out.println("-- partition tests (middle values)"); check("age 30 pays full fare", 400, BuggyFare.fare(30)); check("age 70 gets 40% off", 240, BuggyFare.fare(70));
System.out.println("-- boundary tests"); check("age 59 pays full fare", 400, BuggyFare.fare(59)); check("age 60 gets 40% off", 240, BuggyFare.fare(60)); check("age 61 gets 40% off", 240, BuggyFare.fare(61)); }}-- partition tests (middle values) PASS age 30 pays full fare PASS age 70 gets 40% off -- boundary tests PASS age 59 pays full fare FAIL age 60 gets 40% off: expected 240 but was 400 PASS age 61 gets 40% off
What this code does
<=instead of<moves the edge by one year. Both middle-value tests pass, and so do 59 and 61; only the test exactly at 60 notices that a 60-year-old pays full fare.- That is the case for boundary tests in one picture: the bug affects exactly one input, and that input is the boundary itself.
Example 6 · Partitions and boundaries as a JUnit table
Runs in: a Maven project with JUnit 5
package com.shoplite.practice;
import static org.assertj.core.api.Assertions.assertThat;import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.CsvSource;
class TicketFare { static int fare(int age) { if (age < 0) throw new IllegalArgumentException("Age cannot be negative: " + age); if (age < 5) return 0; if (age < 12) return 200; if (age < 60) return 400; return 240; }}
class TicketFareTest {
@ParameterizedTest(name = "age {0} -> Rs {1}") @CsvSource({ "0, 0", "4, 0", "5, 200", "11, 200", "12, 400", "30, 400", // middle of full fare "59, 400", "60, 240", "61, 240", "120, 240" }) void fareByAge(int age, int expected) { assertThat(TicketFare.fare(age)).isEqualTo(expected); }
@Test void negativeAgeIsRejected() { assertThatThrownBy(() -> TicketFare.fare(-1)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Age cannot be negative: -1"); }}[INFO] Running com.shoplite.practice.TicketFareTest [INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.072 s -- in com.shoplite.practice.TicketFareTest
What this code does
- Several CSV rows per line keep each edge together (
"59, 400", "60, 240"), so the table reads like the rule with its edges marked. - The invalid partition stays a separate exception test. With the
<=bug from Example 5, exactly one row,age 60 -> Rs 240, would turn red.
That is why a good test list has both: one middle value per partition to prove the rule, and the values on each side of every edge to prove where the rule starts.
Find the bug
Find the off-by-one line
Tap the line that is wrong by exactly one.
Rule: delivery is free from Rs 499.
Round 1 of 3Next step
Check your understanding
11 quick questions