Bird
Raised Fist0
JUnittesting~20 mins

@ValueSource for simple values in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit @ValueSource Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a parameterized test with @ValueSource
What will be the output when running this JUnit 5 parameterized test?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SampleTest {
    @ParameterizedTest
    @ValueSource(strings = {"apple", "banana", "cherry"})
    void testStringLength(String fruit) {
        assertTrue(fruit.length() > 3);
    }
}
AAll tests pass successfully.
BTest fails for "cherry" because length is not greater than 3.
CTest fails for "banana" because length is not greater than 3.
DTest fails for "apple" because length is not greater than 3.
Attempts:
2 left
💡 Hint
Check the length of each string in the @ValueSource array.
❓ assertion
intermediate
2:00remaining
Correct assertion for integer values from @ValueSource
Given this parameterized test, which assertion correctly verifies that the input number is even?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;

class NumberTest {
    @ParameterizedTest
    @ValueSource(ints = {2, 4, 6, 7})
    void testIsEven(int number) {
        // Which assertion is correct here?
    }
}
AassertFalse(number % 2 == 0);
BassertTrue(number % 2 == 0);
CassertEquals(1, number % 2);
DassertEquals(0, number / 2);
Attempts:
2 left
💡 Hint
Even numbers have zero remainder when divided by 2.
🔧 Debug
advanced
2:00remaining
Identify the error in this @ValueSource usage
What error will occur when running this test class?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class DebugTest {
    @ParameterizedTest
    @ValueSource(strings = {"one", null, "three"})
    void testStrings(String input) {
        System.out.println(input);
    }
}
ANullPointerException at runtime.
BTest runs successfully printing all values including null.
CCompilation error due to null in @ValueSource.
DIllegalArgumentException: @ValueSource does not support null values.
Attempts:
2 left
💡 Hint
Check if @ValueSource allows null elements in its arrays.
🧠 Conceptual
advanced
2:00remaining
Behavior of @ValueSource with empty arrays
What happens when a parameterized test uses @ValueSource with an empty array?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class EmptyTest {
    @ParameterizedTest
    @ValueSource(strings = {})
    void testEmpty(String input) {
        System.out.println(input);
    }
}
ATest is skipped because there are no values to run.
BTest runs once with input as null.
CTest runs zero times and passes silently.
DTest fails with IllegalArgumentException.
Attempts:
2 left
💡 Hint
Think about how parameterized tests behave with no parameters.
❓ framework
expert
2:00remaining
Combining @ValueSource with other parameter sources
Which statement about combining @ValueSource with other parameter sources in JUnit 5 is correct?
A@ValueSource cannot be combined with other parameter sources; only one source per test method is allowed.
BMultiple @ValueSource annotations can be used on the same test method.
C@ValueSource can be combined with @CsvSource in the same @ParameterizedTest method.
D@ValueSource supports multiple types of values in one annotation (e.g., ints and strings).
Attempts:
2 left
💡 Hint
Check JUnit 5 rules about parameter sources per test method.

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

  1. Step 1: Understand the role of @ValueSource

    @ValueSource provides a list of simple values to run a test repeatedly with each value.
  2. 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.
  3. Final Answer:

    To run the same test multiple times with different simple input values -> Option A
  4. 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

  1. Step 1: Recall correct attribute name for integers

    The attribute for integer arrays in @ValueSource is ints.
  2. Step 2: Check syntax correctness

    @ValueSource(ints = {1, 2, 3}) uses @ValueSource(ints = {1, 2, 3}), which is the correct syntax.
  3. Final Answer:

    @ValueSource(ints = {1, 2, 3}) -> Option D
  4. 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?
@ParameterizedTest
@ValueSource(strings = {"apple", "banana"})
void testLength(String fruit) {
    System.out.println(fruit.length());
}
medium
A. 5 and 6 printed on separate lines
B. 6 and 5 printed on separate lines
C. Only 5 printed once
D. Compilation error due to missing annotation

Solution

  1. Step 1: Identify input values from @ValueSource

    The strings are "apple" (length 5) and "banana" (length 6).
  2. Step 2: Understand test execution

    The test runs twice, printing the length of each string in order: 5 then 6.
  3. Final Answer:

    5 and 6 printed on separate lines -> Option A
  4. Quick Check:

    "apple"=5, "banana"=6 [OK]
Hint: Check string lengths in order given by @ValueSource [OK]
Common Mistakes:
  • Mixing order of outputs
  • Assuming only one run instead of multiple
  • Forgetting @ParameterizedTest annotation
4. Identify the error in this JUnit 5 test using @ValueSource:
@ParameterizedTest
@ValueSource(strings = {"test", null})
void testNotNull(String input) {
    assertNotNull(input);
}
medium
A. Missing @Test annotation
B. assertNotNull is not a valid assertion
C. Using null in @ValueSource causes a runtime error
D. Strings array must not contain more than one value

Solution

  1. Step 1: Check @ValueSource limitations

    @ValueSource does not support null values; including null causes errors.
  2. Step 2: Verify other parts

    assertNotNull is valid, @ParameterizedTest is present, and multiple strings are allowed.
  3. Final Answer:

    Using null in @ValueSource causes a runtime error -> Option C
  4. Quick Check:

    null not allowed in @ValueSource [OK]
Hint: Never put null in @ValueSource arrays [OK]
Common Mistakes:
  • Thinking null is allowed in @ValueSource
  • Confusing @ParameterizedTest with @Test
  • Believing assertNotNull is invalid
5. How can you use @ValueSource to test a method that accepts both int and long values?
hard
A. Use @ValueSource with ints only and cast to long inside the test
B. Use two separate test methods each with @ValueSource for ints and longs
C. Use @ValueSource with longs only and cast to int inside the test
D. Use @ValueSource with mixed int and long arrays in one method

Solution

  1. Step 1: Understand @ValueSource type restrictions

    @ValueSource attributes accept only one primitive type array per annotation (ints or longs, not mixed).
  2. 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.
  3. Final Answer:

    Use two separate test methods each with @ValueSource for ints and longs -> Option B
  4. Quick Check:

    Separate methods for int and long [OK]
Hint: Use separate tests for different primitive types [OK]
Common Mistakes:
  • Trying to mix int and long in one @ValueSource
  • Casting inside test instead of separate methods
  • Assuming @ValueSource supports mixed types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard