Bird
Raised Fist0
JUnittesting~20 mins

@ParameterizedTest annotation in JUnit - Practice Problems & Coding Challenges

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

public class SampleTest {
    @ParameterizedTest
    @ValueSource(strings = {"apple", "banana", "cherry"})
    void testStringLength(String fruit) {
        assertTrue(fruit.length() > 3);
    }
}
AAll tests pass successfully.
BTest fails for "apple" because length is not greater than 3.
CTest fails for "banana" because length is not greater than 3.
DTest fails for "cherry" 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 @ParameterizedTest with @CsvSource
Given this parameterized test, which assertion correctly verifies that the sum of two integers equals the expected result?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;

public class MathTest {
    @ParameterizedTest
    @CsvSource({"1, 2, 3", "4, 5, 9", "10, 20, 30"})
    void testSum(int a, int b, int expected) {
        // Which assertion is correct here?
    }
}
AassertEquals(a + b, expected);
BassertTrue(a + b == expected);
CassertFalse(a + b != expected);
DassertEquals(expected, a + b);
Attempts:
2 left
💡 Hint
Remember the order of parameters in assertEquals(expected, actual).
🔧 Debug
advanced
2:00remaining
Identify the error in this @ParameterizedTest using @MethodSource
What error will occur when running this test class?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.stream.Stream;

public class DebugTest {
    @ParameterizedTest
    @MethodSource("stringProvider")
    void testStrings(String input) {
        assertTrue(input.length() > 0);
    }

    static Stream<String> stringProvider() {
        return Stream.of("");
    }
}
ACompilation error due to missing import for Stream.
BTest passes because empty string length is considered greater than 0.
CTest fails because input string is empty and assertion fails.
DRuntime error due to null pointer exception.
Attempts:
2 left
💡 Hint
Check the assertion condition and the provided input values.
🧠 Conceptual
advanced
2:00remaining
Understanding @ParameterizedTest with multiple parameters
Which statement about @ParameterizedTest with @CsvSource is TRUE?
AEach line in @CsvSource provides values for all parameters of the test method.
B@CsvSource can only provide one parameter per test run.
C@ParameterizedTest cannot be used with @CsvSource.
D@CsvSource automatically converts all parameters to strings only.
Attempts:
2 left
💡 Hint
Think about how multiple values are passed to the test method.
❓ framework
expert
2:00remaining
Behavior of @ParameterizedTest with @EnumSource and filtering
Consider this test using @EnumSource with a filter. Which enum constants will the test run with?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class EnumTest {
    enum Color { RED, GREEN, BLUE, YELLOW }

    @ParameterizedTest
    @EnumSource(value = Color.class, names = {"RED", "BLUE"}, mode = EnumSource.Mode.EXCLUDE)
    void testColors(Color color) {
        assertNotNull(color);
    }
}
AThe test does not run with any colors.
BThe test runs with GREEN and YELLOW only.
CThe test runs with all four colors.
DThe test runs with RED and BLUE only.
Attempts:
2 left
💡 Hint
Check the meaning of mode = EXCLUDE with names specified.

Practice

(1/5)
1. What is the main purpose of the @ParameterizedTest annotation in JUnit?
easy
A. To group multiple test classes together
B. To skip a test method during execution
C. To mark a test method as the only one to run
D. To run the same test multiple times with different inputs

Solution

  1. Step 1: Understand the role of @ParameterizedTest

    The annotation allows running one test method repeatedly with different input values.
  2. Step 2: Compare with other options

    Skipping tests or grouping classes are done by other annotations, not @ParameterizedTest.
  3. Final Answer:

    To run the same test multiple times with different inputs -> Option D
  4. Quick Check:

    @ParameterizedTest = run test with many inputs [OK]
Hint: Think: same test, many inputs, no code repetition [OK]
Common Mistakes:
  • Confusing @ParameterizedTest with @Test
  • Thinking it skips tests
  • Assuming it groups test classes
2. Which of the following is the correct way to provide integer values to a @ParameterizedTest using @ValueSource?
easy
A. @ValueSource(ints = {1, 2, 3})
B. @ValueSource(values = {1, 2, 3})
C. @ValueSource(strings = {1, 2, 3})
D. @ValueSource(numbers = {1, 2, 3})

Solution

  1. Step 1: Recall @ValueSource syntax

    For integers, the correct attribute is 'ints', so @ValueSource(ints = {...}) is correct.
  2. Step 2: Check other options

    'values', 'strings', and 'numbers' are invalid attributes for @ValueSource with integers.
  3. Final Answer:

    @ValueSource(ints = {1, 2, 3}) -> Option A
  4. Quick Check:

    Use 'ints' for integer arrays in @ValueSource [OK]
Hint: Use 'ints' attribute for integer arrays in @ValueSource [OK]
Common Mistakes:
  • Using 'values' instead of 'ints'
  • Using 'strings' for numbers
  • Using unsupported attribute names
3. Given the following test method, what will be the output when running the parameterized test?
@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

    The test runs twice with inputs "apple" and "banana".
  2. Step 2: Calculate string lengths

    "apple" length is 5, "banana" length is 6, so output prints 5 then 6 on separate lines.
  3. Final Answer:

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

    String lengths = 5, 6 [OK]
Hint: Check each input's length printed separately [OK]
Common Mistakes:
  • Mixing order of outputs
  • Expecting only one print
  • Assuming compilation error
4. Identify the error in the following parameterized test code:
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testIsPositive(String number) {
    assertTrue(Integer.parseInt(number) > 0);
}
medium
A. Missing @Test annotation
B. Method parameter type should be int, not String
C. ValueSource should use strings instead of ints
D. assertTrue cannot be used in parameterized tests

Solution

  1. Step 1: Check parameter type vs ValueSource

    @ValueSource provides ints, but method parameter is String, causing type mismatch.
  2. Step 2: Correct parameter type

    Change method parameter to int to match input type and avoid parsing.
  3. Final Answer:

    Method parameter type should be int, not String -> Option B
  4. Quick Check:

    Parameter type must match @ValueSource type [OK]
Hint: Match method parameter type with @ValueSource data type [OK]
Common Mistakes:
  • Using wrong parameter type
  • Forgetting @ParameterizedTest annotation
  • Misusing assertTrue in parameterized tests
5. You want to test a method that checks if a number is even using @ParameterizedTest with multiple inputs including 0, -2, and 3. Which of the following test method signatures and annotations correctly achieve this?
hard
A. @ParameterizedTest @ValueSource(ints = {0, -2, 3}) void testIsEven(int number) { assertEquals(number % 2, 0); }
B. @ParameterizedTest @ValueSource(ints = {0, -2, 3}) void testIsEven(int number) { assertEquals(0, number % 2); }
C. @ParameterizedTest @CsvSource({"0, true", "-2, true", "3, false"}) void testIsEven(int number, boolean expected) { assertEquals(expected, number % 2 == 0); }
D. @ParameterizedTest @CsvSource({"0, true", "-2, true", "3, false"}) void testIsEven(String number, boolean expected) { assertEquals(expected, Integer.parseInt(number) % 2 == 0); }

Solution

  1. Step 1: Understand input and expected output

    We want to test numbers with expected boolean results, so two parameters are needed.
  2. Step 2: Check annotation and method signature

    @CsvSource allows multiple parameters; method parameters match types (int, boolean) and assertion compares expected with actual.
  3. Step 3: Analyze other options

    Options A and B have incorrect assertEquals argument order or logic; D uses String parameter unnecessarily.
  4. Final Answer:

    Option C correctly uses @CsvSource with matching parameters and assertion -> Option C
  5. Quick Check:

    Use @CsvSource for multiple inputs and expected results [OK]
Hint: Use @CsvSource for inputs plus expected results [OK]
Common Mistakes:
  • Using wrong assertEquals argument order
  • Mismatching parameter types
  • Using @ValueSource when multiple parameters needed
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