Parameterization helps run the same test with different data without writing the test again and again. This saves time and keeps tests simple.
Why parameterization reduces test duplication in JUnit
@ParameterizedTest
@ValueSource(strings = {"input1", "input2", "input3"})
void testWithParameters(String input) {
// test code using input
}@ParameterizedTest tells JUnit to run the test multiple times with different data.
@ValueSource provides the data values for the test method parameters.
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testNumbers(int number) {
assertTrue(number > 0);
}@ParameterizedTest
@CsvSource({"apple, 5", "banana, 6", "cherry, 6"})
void testFruitLength(String fruit, int length) {
assertEquals(length, fruit.length());
}This test checks that none of the given strings are empty. It runs three times with different words.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class ParameterizedTestExample { @ParameterizedTest @ValueSource(strings = {"hello", "world", "JUnit"}) void testStringNotEmpty(String word) { assertFalse(word.isEmpty()); } }
Parameterization reduces code repetition by reusing the same test logic with different inputs.
It makes tests easier to read and maintain because you only change data, not code.
JUnit 5 provides many ways to supply parameters like @ValueSource, @CsvSource, and @MethodSource.
Parameterization runs one test multiple times with different data.
This avoids writing the same test many times for different inputs.
It keeps tests clean, simple, and easy to update.
Practice
Solution
Step 1: Understand parameterization purpose
Parameterization runs the same test method repeatedly with different input values.Step 2: Compare with duplication
This avoids writing separate test methods for each input, reducing repeated code.Final Answer:
It allows running the same test method multiple times with different inputs. -> Option AQuick Check:
Parameterization = multiple inputs, one test method [OK]
- Thinking parameterization fixes bugs automatically
- Confusing parameterization with random data generation
- Believing it merges test classes
Solution
Step 1: Identify correct annotation for parameterized tests
JUnit 5 uses @ParameterizedTest to mark parameterized test methods.Step 2: Check data source annotation
@ValueSource provides input values; it must be combined with @ParameterizedTest.Final Answer:
@ParameterizedTest with @ValueSource is correct syntax. -> Option BQuick Check:
@ParameterizedTest + @ValueSource = correct syntax [OK]
- Using @Test instead of @ParameterizedTest
- Using non-existent @Parameters or @TestSource annotations
- Mixing annotations incorrectly
@ParameterizedTest
@ValueSource(ints = {2, 4, 6})
void testIsEven(int number) {
assertTrue(number % 2 == 0);
}Solution
Step 1: Analyze input values
The input values are 2, 4, and 6, all even numbers.Step 2: Check assertion logic
The assertion checks if number % 2 == 0, which is true for all inputs.Final Answer:
All tests pass because all numbers are even. -> Option CQuick Check:
All inputs even -> assertion true -> tests pass [OK]
- Assuming some numbers are odd without checking
- Confusing assertion logic
- Thinking syntax error exists when it does not
@ParameterizedTest
@ValueSource(strings = {"test", "JUnit"})
void testLength(int length) {
assertTrue(length > 0);
}Solution
Step 1: Check parameter type against @ValueSource data
@ValueSource provides strings, but method parameter is int, causing a type mismatch.Step 2: Understand correct parameter type
The method parameter should be String to match the input data type.Final Answer:
The parameter type should be String, not int. -> Option DQuick Check:
Parameter type must match @ValueSource data type [OK]
- Using wrong parameter type
- Forgetting parentheses in annotations (not the case here)
- Thinking test methods must return values
- Believing assertTrue is disallowed in parameterized tests
Solution
Step 1: Understand test duplication problem
Multiple test methods for positive, negative, zero numbers cause repeated code.Step 2: Apply parameterization to combine tests
Use one parameterized test method with inputs and expected results to cover all cases.Final Answer:
By combining all checks into one parameterized test method with input and expected result parameters. -> Option AQuick Check:
One method + multiple inputs = less duplication [OK]
- Writing separate test classes instead of parameterizing
- Removing assertions which breaks test automation
- Testing only one fixed input
Ready to prove it?
Pass the test and this pattern moves unknown
