Use @ValueSource only for simple, single-parameter values.
For multiple parameters, use other providers like @CsvSource.
Keep your test method parameter type matching the @ValueSource type exactly.
Summary
@ValueSource runs one test multiple times with simple values.
It helps test many inputs without repeating code.
Works well for ints, strings, chars, doubles, and longs.
Practice
(1/5)
1. What is the main purpose of using @ValueSource in JUnit tests?
easy
A. To run the same test multiple times with different simple input values
B. To create a new test class automatically
C. To skip tests based on conditions
D. To generate random test data during runtime
Solution
Step 1: Understand the role of @ValueSource
@ValueSource provides a list of simple values to run a test repeatedly with each value.
Step 2: Compare options with this role
Only To run the same test multiple times with different simple input values correctly describes running the same test multiple times with different inputs.
Final Answer:
To run the same test multiple times with different simple input values -> Option A
Quick Check:
@ValueSource = run test multiple times [OK]
Hint: Remember: @ValueSource repeats tests with simple inputs [OK]
Common Mistakes:
Thinking @ValueSource creates new test classes
Confusing @ValueSource with conditional test skipping
Assuming it generates random data
2. Which of the following is the correct syntax to use @ValueSource with integers in JUnit 5?
easy
A. @ValueSource(numbers = {1, 2, 3})
B. @ValueSource(values = {1, 2, 3})
C. @ValueSource(int = {1, 2, 3})
D. @ValueSource(ints = {1, 2, 3})
Solution
Step 1: Recall correct attribute name for integers
The attribute for integer arrays in @ValueSource is ints.
Step 2: Check syntax correctness
@ValueSource(ints = {1, 2, 3}) uses @ValueSource(ints = {1, 2, 3}), which is the correct syntax.
Final Answer:
@ValueSource(ints = {1, 2, 3}) -> Option D
Quick Check:
ints attribute = correct syntax [OK]
Hint: Use 'ints' attribute for integer arrays in @ValueSource [OK]
Common Mistakes:
Using wrong attribute names like 'values' or 'numbers'
Missing 's' in 'ints'
Using singular 'int' instead of 'ints'
3. Given the following test method, what will be the output when run?