What if you could test dozens of cases with just one simple test method?
Why @ParameterizedTest annotation in JUnit? - Purpose & Use Cases
Imagine you have a calculator app and want to test its addition function with many pairs of numbers. You write a separate test for each pair manually.
Writing many similar tests one by one is slow and boring. It's easy to make mistakes or forget some cases. Running all tests takes longer and the code becomes messy.
The @ParameterizedTest annotation lets you run the same test logic with different inputs automatically. You write the test once and provide many data sets. This saves time and keeps tests clean.
void testAdd1() { assertEquals(3, add(1, 2)); }
void testAdd2() { assertEquals(5, add(2, 3)); }@ParameterizedTest
@CsvSource({"1, 2, 3", "2, 3, 5"})
void testAdd(int a, int b, int expected) { assertEquals(expected, add(a, b)); }You can easily test many input cases with less code, making your tests faster, clearer, and less error-prone.
Testing a login form with many username and password combinations to ensure all valid and invalid inputs behave correctly.
Manual repetitive tests are slow and error-prone.
@ParameterizedTest runs one test with many inputs automatically.
This makes testing faster, cleaner, and more reliable.
Practice
@ParameterizedTest annotation in JUnit?Solution
Step 1: Understand the role of @ParameterizedTest
The annotation allows running one test method repeatedly with different input values.Step 2: Compare with other options
Skipping tests or grouping classes are done by other annotations, not @ParameterizedTest.Final Answer:
To run the same test multiple times with different inputs -> Option DQuick Check:
@ParameterizedTest = run test with many inputs [OK]
- Confusing @ParameterizedTest with @Test
- Thinking it skips tests
- Assuming it groups test classes
@ParameterizedTest using @ValueSource?Solution
Step 1: Recall @ValueSource syntax
For integers, the correct attribute is 'ints', so @ValueSource(ints = {...}) is correct.Step 2: Check other options
'values', 'strings', and 'numbers' are invalid attributes for @ValueSource with integers.Final Answer:
@ValueSource(ints = {1, 2, 3}) -> Option AQuick Check:
Use 'ints' for integer arrays in @ValueSource [OK]
- Using 'values' instead of 'ints'
- Using 'strings' for numbers
- Using unsupported attribute names
@ParameterizedTest
@ValueSource(strings = {"apple", "banana"})
void testLength(String fruit) {
System.out.println(fruit.length());
}Solution
Step 1: Identify input values
The test runs twice with inputs "apple" and "banana".Step 2: Calculate string lengths
"apple" length is 5, "banana" length is 6, so output prints 5 then 6 on separate lines.Final Answer:
5 and 6 printed on separate lines -> Option AQuick Check:
String lengths = 5, 6 [OK]
- Mixing order of outputs
- Expecting only one print
- Assuming compilation error
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testIsPositive(String number) {
assertTrue(Integer.parseInt(number) > 0);
}Solution
Step 1: Check parameter type vs ValueSource
@ValueSource provides ints, but method parameter is String, causing type mismatch.Step 2: Correct parameter type
Change method parameter to int to match input type and avoid parsing.Final Answer:
Method parameter type should be int, not String -> Option BQuick Check:
Parameter type must match @ValueSource type [OK]
- Using wrong parameter type
- Forgetting @ParameterizedTest annotation
- Misusing assertTrue in parameterized tests
@ParameterizedTest with multiple inputs including 0, -2, and 3. Which of the following test method signatures and annotations correctly achieve this?Solution
Step 1: Understand input and expected output
We want to test numbers with expected boolean results, so two parameters are needed.Step 2: Check annotation and method signature
@CsvSource allows multiple parameters; method parameters match types (int, boolean) and assertion compares expected with actual.Step 3: Analyze other options
Options A and B have incorrect assertEquals argument order or logic; D uses String parameter unnecessarily.Final Answer:
Option C correctly uses @CsvSource with matching parameters and assertion -> Option CQuick Check:
Use @CsvSource for multiple inputs and expected results [OK]
- Using wrong assertEquals argument order
- Mismatching parameter types
- Using @ValueSource when multiple parameters needed
Ready to prove it?
Pass the test and this pattern moves unknown
