Sub topic 9
Running One Test with Many Inputs
Sometimes you want to check the same behavior with many different values. You could copy the same test five times and change the numbers each time, but that makes the test file long and easy to get wrong. JUnit gives you a better tool: the parameterized test.
What Is a Parameterized Test?
A parameterized test is a test method that runs many times, once for each set of input values. A parameter is a value passed into a method. JUnit supplies the parameters, and an argument source tells JUnit where to get them. An argument source is an annotation that lists the input values.
You write the test once. JUnit runs it once per input.
Why It Matters
- You avoid copy-and-paste tests.
- Adding a new case takes one line.
- Each input shows up as its own result, so you can see exactly which one failed.
A Worked Example with @CsvSource
The Calculator class has an add method. Here is one test that checks three different sums:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class CalculatorParameterizedTest {
private final Calculator calculator = new Calculator();
@ParameterizedTest
@CsvSource({
"1, 1, 2",
"2, 3, 5",
"-4, 4, 0"
})
void addHandlesManyInputs(int a, int b, int expected) {
assertEquals(expected, calculator.add(a, b));
}
}The test works like this:
@ParameterizedTestreplaces@Test. It tells JUnit that this method takes inputs.@CsvSourceis the argument source. CSV means "comma-separated values." Each string is one row.- JUnit splits each row at the commas. The first column goes to
a, the second tob, and the third toexpected. - JUnit turns the text
"2"into theintvalue2for you.
When you run this class, your IDE shows three results under one test name. If the second row fails, the other two still run and report on their own.
A Simpler Source: @ValueSource
If your test needs only one value per run, use @ValueSource. It takes a plain list of values.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class AddZeroTest {
private final Calculator calculator = new Calculator();
@ParameterizedTest
@ValueSource(ints = {0, 7, -3, 100})
void addingZeroKeepsTheNumber(int n) {
assertEquals(n, calculator.add(n, 0));
}
}@ValueSource also accepts strings, longs, doubles, and other simple types.
Common Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Using `@Test` together with `@ParameterizedTest` | JUnit tries to run the method without inputs and reports an error | Use only `@ParameterizedTest` |
| Wrong number of columns in a row | The test fails with a parameter resolution error | Make every row match the method's parameters |
| Forgetting the argument source | JUnit reports that no arguments were provided | Add `@CsvSource`, `@ValueSource`, or another source |
Exercise
Write a parameterized test that checks calculator.divide with the rows "10, 2, 5", "9, 3, 3", and "-8, 2, -4". Which annotations do you need, and what are the method's parameters?
Show answer
Use @ParameterizedTest and @CsvSource({"10, 2, 5", "9, 3, 3", "-8, 2, -4"}) on a method such as void divideHandlesManyInputs(int a, int b, int expected). Inside it, call assertEquals(expected, calculator.divide(a, b));. JUnit runs the method three times, once per row.
Exercise
You need to check that calculator.add(n, n) equals n * 2 for the values 1, 5, and 12. Which argument source is the simplest choice, and why?
Show answer
@ValueSource(ints = {1, 5, 12}), because each run needs only one input value. The method takes one int n parameter and asserts assertEquals(n * 2, calculator.add(n, n));.