Writing
Test Cases in Java
Learn how to write simple test cases and predict expected outputs for Java methods: a foundational skill for building reliable software.
What Is a Test Case?
A test case is a specific scenario you create to verify that a method behaves correctly. It consists of three parts: the input you provide, the action you perform (calling the method), and the expected output you predict.
Why Write Tests?
Testing is not just for big companies. Even in a classroom, writing test cases helps you think through your logic before and after coding. It catches bugs early and builds confidence that your code works.
Anatomy of a Simple Test
The Golden Rule of Testing
Test Cases vs Debugging
| Test Cases | Debugging | |
|---|---|---|
| When | Before and after writing code | After a bug is found |
| Purpose | Prevent bugs proactively | Fix bugs reactively |
| Approach | Systematic: planned inputs and outputs | Investigative: trace execution flow |
| Reusable? | Yes: run again after changes | No: one-time effort |
How to Write a Test Case
Choosing Good Inputs
Not all inputs are equally useful. A strong set of test cases covers normal cases, edge cases, and boundary values.
7 to a method that finds the absolute value.0, an empty string, or null.Integer.MAX_VALUE, first/last index of an array.Example 1: Testing isEven
isEven(4) → expected: trueisEven(7) → expected: falseisEven(0) → expected: trueisEven(-3) → expected: falseExample 1: Test Code
Example 2: Testing maxOfThree
maxOfThree(10, 5, 3) → expected: 10maxOfThree(2, 9, 4) → expected: 9maxOfThree(1, 3, 7) → expected: 7maxOfThree(5, 5, 5) → expected: 5maxOfThree(-1, -5, -2) → expected: -1Example 3: Testing countVowels
countVowels("hello") → expected: 2countVowels("aeiou") → expected: 5countVowels("xyz") → expected: 0countVowels("") → expected: 0countVowels("ApPlE") → expected: 2The Power of Edge Cases
Edge cases are the inputs most likely to reveal bugs. They live at the boundaries of what your method handles. Always ask: "What happens at the extremes?"
A Reusable Test Helper
Instead of writing println for every check, create a small helper method that prints a clear pass/fail message:
Common Mistakes in Test Cases
0, negatives, empty strings, or null.Test Case Checklist
| Category | What to Test | Example |
|---|---|---|
| Normal Case | Typical, expected values | isEven(4) → true |
| Edge Case (Zero) | An extreme or minimal value | isEven(0) → true |
| Edge Case (Empty) | Empty string or collection | countVowels("") → 0 |
| Negative Numbers | Values outside the positive domain | maxOfThree(-1,-5,-2) → -1 |
| Equal Inputs | All inputs are identical | maxOfThree(5,5,5) → 5 |
Question 1 of 5
Given the method public static int abs(int n), which returns the absolute value of n, what is the expected output of abs(-7)?
Question 2 of 5
Which of the following is the best edge case to test for a method public static double average(int[] arr)?
Question 3 of 5
Method:
public static String greet(String name)
Returns "Hello, " concatenated with the given name. For example, greet("Alice") returns "Hello, Alice".
A student writes this test: greet("Bob") returns "Hello, Bob". What is the biggest weakness of testing only this case?
Question 4 of 5
In the Arrange-Act-Assert pattern, what does the "Assert" step do?
Question 5 of 5
Method:
public static int max(int a, int b)
Returns the larger of the two integers. If they are equal, returns that value.
Test code:
System.out.println(max(3, 3) == 3);
System.out.println(max(5, 2) == 5);
System.out.println(max(1, 9) == 9);
Which additional test case would most improve this test suite?