0
0
JUnittesting~20 mins

@ParameterizedTest annotation in JUnit - Practice Problems & Coding Challenges

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