What if you could test many inputs with just one simple method and no copy-pasting?
Why @ValueSource for simple values in JUnit? - Purpose & Use Cases
Imagine you have a function that needs to be tested with many different input values. You write a separate test method for each value, repeating almost the same code again and again.
This manual way is slow and boring. It's easy to make mistakes by copying and pasting. Also, if you want to add more test values, you have to write even more methods, making your test code messy and hard to maintain.
@ValueSource lets you run the same test method multiple times with different simple values automatically. You write the test once and just list the values. JUnit takes care of running the test for each value, saving time and avoiding errors.
void testWithOneValue() { assertTrue(isValid(1)); }
void testWithAnotherValue() { assertTrue(isValid(2)); }@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testWithMultipleValues(int number) {
assertTrue(isValid(number));
}You can easily test many input values with one clean, simple test method, making your tests faster to write and easier to understand.
Testing a password validator with different password lengths or characters to ensure it works correctly for all cases without writing many separate tests.
Manual tests for each value are slow and repetitive.
@ValueSource runs one test multiple times with different values.
This makes tests simpler, cleaner, and less error-prone.
Practice
@ValueSource in JUnit tests?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 AQuick Check:
@ValueSource = run test multiple times [OK]
- Thinking @ValueSource creates new test classes
- Confusing @ValueSource with conditional test skipping
- Assuming it generates random data
@ValueSource with integers in JUnit 5?Solution
Step 1: Recall correct attribute name for integers
The attribute for integer arrays in @ValueSource isints.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 DQuick Check:
ints attribute = correct syntax [OK]
- Using wrong attribute names like 'values' or 'numbers'
- Missing 's' in 'ints'
- Using singular 'int' instead of 'ints'
@ParameterizedTest
@ValueSource(strings = {"apple", "banana"})
void testLength(String fruit) {
System.out.println(fruit.length());
}Solution
Step 1: Identify input values from @ValueSource
The strings are "apple" (length 5) and "banana" (length 6).Step 2: Understand test execution
The test runs twice, printing the length of each string in order: 5 then 6.Final Answer:
5 and 6 printed on separate lines -> Option AQuick Check:
"apple"=5, "banana"=6 [OK]
- Mixing order of outputs
- Assuming only one run instead of multiple
- Forgetting @ParameterizedTest annotation
@ValueSource:@ParameterizedTest
@ValueSource(strings = {"test", null})
void testNotNull(String input) {
assertNotNull(input);
}Solution
Step 1: Check @ValueSource limitations
@ValueSource does not support null values; including null causes errors.Step 2: Verify other parts
assertNotNull is valid, @ParameterizedTest is present, and multiple strings are allowed.Final Answer:
Using null in @ValueSource causes a runtime error -> Option CQuick Check:
null not allowed in @ValueSource [OK]
- Thinking null is allowed in @ValueSource
- Confusing @ParameterizedTest with @Test
- Believing assertNotNull is invalid
@ValueSource to test a method that accepts both int and long values?Solution
Step 1: Understand @ValueSource type restrictions
@ValueSource attributes accept only one primitive type array per annotation (ints or longs, not mixed).Step 2: Choose approach to test both types
To test both int and long inputs, create two test methods each with its own @ValueSource for the correct type.Final Answer:
Use two separate test methods each with @ValueSource for ints and longs -> Option BQuick Check:
Separate methods for int and long [OK]
- Trying to mix int and long in one @ValueSource
- Casting inside test instead of separate methods
- Assuming @ValueSource supports mixed types
Ready to prove it?
Pass the test and this pattern moves unknown
