0
0
JUnittesting~20 mins

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

Choose your learning style9 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.