Bird
Raised Fist0
JUnittesting~20 mins

Why parameterization reduces test duplication in JUnit - Challenge Your Understanding

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

Practice

(1/5)
1. Why does parameterization in JUnit help reduce test duplication?
easy
A. It allows running the same test method multiple times with different inputs.
B. It automatically fixes bugs in the test code.
C. It generates random test data for each test run.
D. It merges multiple test classes into one.

Solution

  1. Step 1: Understand parameterization purpose

    Parameterization runs the same test method repeatedly with different input values.
  2. Step 2: Compare with duplication

    This avoids writing separate test methods for each input, reducing repeated code.
  3. Final Answer:

    It allows running the same test method multiple times with different inputs. -> Option A
  4. Quick Check:

    Parameterization = multiple inputs, one test method [OK]
Hint: Parameterization runs one test many times with different data [OK]
Common Mistakes:
  • Thinking parameterization fixes bugs automatically
  • Confusing parameterization with random data generation
  • Believing it merges test classes
2. Which of the following is the correct JUnit 5 syntax to create a parameterized test method?
easy
A. @Test @ValueSource(strings = {"apple", "banana"}) void testFruit(String fruit) { }
B. @ParameterizedTest @ValueSource(strings = {"apple", "banana"}) void testFruit(String fruit) { }
C. @Test @Parameters({"apple", "banana"}) void testFruit(String fruit) { }
D. @ParameterizedTest @TestSource(strings = {"apple", "banana"}) void testFruit(String fruit) { }

Solution

  1. Step 1: Identify correct annotation for parameterized tests

    JUnit 5 uses @ParameterizedTest to mark parameterized test methods.
  2. Step 2: Check data source annotation

    @ValueSource provides input values; it must be combined with @ParameterizedTest.
  3. Final Answer:

    @ParameterizedTest with @ValueSource is correct syntax. -> Option B
  4. Quick Check:

    @ParameterizedTest + @ValueSource = correct syntax [OK]
Hint: Use @ParameterizedTest with @ValueSource for parameterized tests [OK]
Common Mistakes:
  • Using @Test instead of @ParameterizedTest
  • Using non-existent @Parameters or @TestSource annotations
  • Mixing annotations incorrectly
3. Given the following JUnit 5 parameterized test, what will be the output when running the test?
@ParameterizedTest
@ValueSource(ints = {2, 4, 6})
void testIsEven(int number) {
    assertTrue(number % 2 == 0);
}
medium
A. Some tests pass and some fail depending on the number.
B. All tests fail because the assertion is incorrect.
C. All tests pass because all numbers are even.
D. Test will not run due to syntax error.

Solution

  1. Step 1: Analyze input values

    The input values are 2, 4, and 6, all even numbers.
  2. Step 2: Check assertion logic

    The assertion checks if number % 2 == 0, which is true for all inputs.
  3. Final Answer:

    All tests pass because all numbers are even. -> Option C
  4. Quick Check:

    All inputs even -> assertion true -> tests pass [OK]
Hint: Check if all inputs satisfy the assertion condition [OK]
Common Mistakes:
  • Assuming some numbers are odd without checking
  • Confusing assertion logic
  • Thinking syntax error exists when it does not
4. Identify the error in this JUnit 5 parameterized test code:
@ParameterizedTest
@ValueSource(strings = {"test", "JUnit"})
void testLength(int length) {
    assertTrue(length > 0);
}
medium
A. assertTrue cannot be used in parameterized tests.
B. The @ValueSource annotation is missing parentheses.
C. The test method must return a value.
D. The parameter type should be String, not int.

Solution

  1. Step 1: Check parameter type against @ValueSource data

    @ValueSource provides strings, but method parameter is int, causing a type mismatch.
  2. Step 2: Understand correct parameter type

    The method parameter should be String to match the input data type.
  3. Final Answer:

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

    Parameter type must match @ValueSource data type [OK]
Hint: Match method parameter type with @ValueSource data type [OK]
Common Mistakes:
  • Using wrong parameter type
  • Forgetting parentheses in annotations (not the case here)
  • Thinking test methods must return values
  • Believing assertTrue is disallowed in parameterized tests
5. You have multiple test methods checking if numbers are positive, negative, or zero. How does parameterization reduce duplication in this scenario?
hard
A. By combining all checks into one parameterized test method with input and expected result parameters.
B. By writing separate test classes for each number type.
C. By removing assertions and relying on manual checks.
D. By running tests only once with a fixed number.

Solution

  1. Step 1: Understand test duplication problem

    Multiple test methods for positive, negative, zero numbers cause repeated code.
  2. Step 2: Apply parameterization to combine tests

    Use one parameterized test method with inputs and expected results to cover all cases.
  3. Final Answer:

    By combining all checks into one parameterized test method with input and expected result parameters. -> Option A
  4. Quick Check:

    One method + multiple inputs = less duplication [OK]
Hint: Use one test method with inputs and expected outputs [OK]
Common Mistakes:
  • Writing separate test classes instead of parameterizing
  • Removing assertions which breaks test automation
  • Testing only one fixed input
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