0
0
JUnittesting~20 mins

Why parameterization reduces test duplication in JUnit - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parameterization Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does parameterization reduce test duplication?

Imagine you have multiple tests that check the same logic but with different input values. How does parameterization help reduce duplication in such cases?

AIt replaces all test inputs with a single fixed value to simplify testing.
BIt merges all tests into one big test that runs all inputs at once without separation.
CIt automatically generates new test methods for each input without needing code changes.
DIt allows running the same test method multiple times with different inputs, avoiding writing separate methods for each case.
Attempts:
2 left
💡 Hint

Think about how you can reuse the same code but test different values.

Predict Output
intermediate
2:00remaining
Output of parameterized JUnit test run

Given this JUnit 5 parameterized test, what will be the output when running it?

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 = {"racecar", "radar", "hello"})
    void testPalindrome(String word) {
        assertTrue(new StringBuilder(word).reverse().toString().equals(word));
    }
}
AAll tests pass because all words are palindromes.
BTest fails because 'hello' is not a palindrome.
CTest fails due to syntax error in annotation.
DTest is skipped because no test method is found.
Attempts:
2 left
💡 Hint

Check which input strings are palindromes.

assertion
advanced
2:00remaining
Correct assertion in parameterized test

Which assertion correctly verifies that a number is even inside a JUnit 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 NumberTest {
    @ParameterizedTest
    @ValueSource(ints = {2, 4, 5, 8})
    void testEven(int number) {
        // Which assertion is correct here?
    }
}
AassertTrue(number % 2 == 0);
BassertEquals(number / 2, 0);
CassertNull(number % 2);
DassertFalse(number % 2 == 0);
Attempts:
2 left
💡 Hint

Think about how to check if a number is even using modulo.

🔧 Debug
advanced
2:00remaining
Why does this parameterized test fail to run?

Look at this JUnit 5 parameterized test code. Why does it fail to run any tests?

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class DebugTest {
    @ParameterizedTest
    @ValueSource(strings = {"apple", "banana"})
    void testFruit() {
        System.out.println("Fruit test");
    }
}
AThe test method must be static to run parameterized tests.
BThe @ValueSource annotation is missing required attributes.
CThe test method must accept a parameter matching the ValueSource type.
DJUnit 5 does not support @ParameterizedTest with @ValueSource.
Attempts:
2 left
💡 Hint

Check the method signature and parameters.

framework
expert
2:00remaining
How does JUnit 5 parameterized test runner handle multiple inputs?

In JUnit 5, when using @ParameterizedTest with multiple inputs, how does the test runner execute the tests?

AIt runs the test method once for each input value, reporting each as a separate test case.
BIt combines all inputs into one test run and reports a single pass/fail result.
CIt randomly selects one input to run per test execution.
DIt requires manual looping inside the test method to run all inputs.
Attempts:
2 left
💡 Hint

Think about how parameterized tests show results in test reports.