Bird
Raised Fist0
JUnittesting~20 mins

Multiple parameter types in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit Multiple Parameter Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of JUnit test with multiple parameter types
Consider the following JUnit 5 parameterized test method using @CsvSource with mixed parameter types. What will be the test execution result for the given inputs?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    @ParameterizedTest
    @CsvSource({
        "2, 3, 5",
        "-1, 1, 0",
        "0, 0, 0",
        "5, -3, 2"
    })
    void testAdd(int a, int b, int expected) {
        assertEquals(expected, a + b);
    }
}
AThe test with inputs (0, 0, 0) throws a runtime exception.
BThe test with inputs (5, -3, 2) fails due to wrong expected value.
CThe test with inputs (-1, 1, 0) fails due to type mismatch error.
DAll 4 tests pass successfully.
Attempts:
2 left
💡 Hint
Check the addition results for each input set carefully.
❓ assertion
intermediate
1:30remaining
Correct assertion for mixed parameter types in JUnit
You have a parameterized test method with parameters: String name, int age, boolean active. Which assertion correctly verifies that age is greater than 18 and active is true?
JUnit
import static org.junit.jupiter.api.Assertions.*;

void testUser(String name, int age, boolean active) {
    // Which assertion is correct?
}
AassertFalse(age <= 18 || !active);
BassertEquals(true, age > 18 && active);
CassertTrue(age > 18 && active);
DassertTrue(age > 18 || active);
Attempts:
2 left
💡 Hint
Use assertTrue with the exact condition you want to verify.
🔧 Debug
advanced
2:30remaining
Identify the cause of failure in mixed parameter JUnit test
Given this parameterized test using @MethodSource, why does the test fail with a ParameterResolutionException?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;

class UserTest {
    static Stream<Object[]> userProvider() {
        return Stream.of(
            new Object[]{"Alice", 30, true},
            new Object[]{"Bob", "twenty", false}
        );
    }

    @ParameterizedTest
    @MethodSource("userProvider")
    void testUser(String name, int age, boolean active) {
        // test logic
    }
}
AThe second parameter '"twenty"' cannot be converted to int, causing the failure.
BThe method userProvider() is not static, causing the failure.
CThe test method parameters do not match the provider method return type.
DThe boolean parameter 'active' is not boxed, causing the failure.
Attempts:
2 left
💡 Hint
Check the types of values provided for each parameter.
❓ framework
advanced
1:30remaining
JUnit 5 parameterized test with multiple parameter types and custom converter
Which JUnit 5 feature allows you to convert a String parameter to a custom object type in a parameterized test?
A@ConvertWith annotation with a custom ArgumentConverter implementation
B@CsvSource with inline lambda conversion
C@ValueSource with a custom parser method
D@MethodSource with automatic type inference
Attempts:
2 left
💡 Hint
Look for annotations that handle parameter conversion explicitly.
🧠 Conceptual
expert
3:00remaining
Handling multiple parameter types in JUnit parameterized tests with null values
In a JUnit 5 parameterized test with parameters (String name, Integer age, Boolean active), which approach correctly allows passing null values for age and active using @CsvSource?
AUse empty strings for nulls and annotate the test method with @NullAndEmptySource
BUse the string 'null' in @CsvSource and annotate parameters with @ConvertWith(NullConverter.class)
CUse the string 'null' in @CsvSource and annotate the test method with @NullSource
DUse the string 'null' in @CsvSource and annotate parameters with @NullAndEmptySource
Attempts:
2 left
💡 Hint
Consider how JUnit converts string literals to null for non-String parameters.

Practice

(1/5)
1. What is the main benefit of using @ParameterizedTest with multiple parameter types in JUnit?
easy
A. It makes tests run faster by skipping assertions.
B. It automatically fixes bugs in the tested code.
C. It allows running the same test method with different input values easily.
D. It generates test reports without writing any code.

Solution

  1. Step 1: Understand the purpose of @ParameterizedTest

    @ParameterizedTest lets you run one test method multiple times with different inputs.
  2. Step 2: Recognize the benefit of multiple parameter types

    Using multiple parameter types means you can test various input combinations easily, improving coverage.
  3. Final Answer:

    It allows running the same test method with different input values easily. -> Option C
  4. Quick Check:

    @ParameterizedTest runs tests with many inputs = A [OK]
Hint: Think: same test, many inputs, easy coverage [OK]
Common Mistakes:
  • Confusing parameterized tests with automatic bug fixing
  • Believing it skips assertions to run faster
  • Assuming it generates reports without code
2. Which of the following is the correct syntax to provide multiple parameters of different types using @CsvSource in JUnit?
easy
A. @CsvSource({"1, 'test', true", "2, 'check', false"})
B. @CsvSource(1, 'test', true; 2, 'check', false)
C. @CsvSource([1, 'test', true], [2, 'check', false])
D. @CsvSource("1; 'test'; true", "2; 'check'; false")

Solution

  1. Step 1: Recall the correct @CsvSource format

    @CsvSource expects an array of strings, each string with comma-separated values.
  2. Step 2: Check each option's syntax

    @CsvSource({"1, 'test', true", "2, 'check', false"}) uses curly braces and comma-separated values inside quotes, which is correct syntax.
  3. Final Answer:

    @CsvSource({"1, 'test', true", "2, 'check', false"}) -> Option A
  4. Quick Check:

    @CsvSource uses braces and comma-separated strings = C [OK]
Hint: Look for braces and comma-separated quoted strings [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Not enclosing the whole list in braces
  • Using square brackets instead of braces
3. Given the following JUnit test method, what will be the output when it runs?
@ParameterizedTest
@CsvSource({"2, 3, 5", "4, 5, 9", "1, 1, 3"})
void testSum(int a, int b, int expected) {
    assertEquals(expected, a + b);
}
medium
A. Syntax error prevents tests from running.
B. All tests pass.
C. All tests fail.
D. Two tests pass, one test fails.

Solution

  1. Step 1: Check each test case's expected vs actual sum

    Test 1: 2+3=5 matches expected 5 (pass). Test 2: 4+5=9 matches expected 9 (pass). Test 3: 1+1=2 but expected 3 (fail).
  2. Step 2: Determine overall test results

    Two tests pass, one test fails due to wrong expected value.
  3. Final Answer:

    Two tests pass, one test fails. -> Option D
  4. Quick Check:

    Sum matches expected except last = A [OK]
Hint: Add inputs and compare to expected for each case [OK]
Common Mistakes:
  • Assuming all pass without checking expected values
  • Ignoring the failing test case
  • Thinking syntax error occurs due to quotes
4. Identify the error in this JUnit parameterized test using multiple parameter types:
@ParameterizedTest
@CsvSource({"10, 20", "30, 40"})
void testMultiply(int a, int b, int expected) {
    assertEquals(expected, a * b);
}
medium
A. The @CsvSource syntax is incorrect; it should use semicolons instead of commas.
B. The method expects three parameters but only two are provided in @CsvSource.
C. The assertEquals arguments are reversed.
D. The test method should not use @ParameterizedTest with @CsvSource.

Solution

  1. Step 1: Compare method parameters with @CsvSource values

    The method expects three parameters (int a, int b, int expected), but each @CsvSource entry has only two values.
  2. Step 2: Identify mismatch causing error

    Missing the third parameter in @CsvSource causes a runtime error due to parameter count mismatch.
  3. Final Answer:

    The method expects three parameters but only two are provided in @CsvSource. -> Option B
  4. Quick Check:

    Parameter count mismatch = B [OK]
Hint: Count parameters in method and CsvSource entries [OK]
Common Mistakes:
  • Thinking commas are wrong separators
  • Assuming assertEquals argument order causes error
  • Believing @ParameterizedTest can't be used with @CsvSource
5. You want to test a method isValidUser(String username, int age, boolean active) using @ParameterizedTest with multiple parameter types. Which @CsvSource entry correctly represents a test case with username "alice", age 30, and active status true?
hard
A. "alice, 30, true"
B. "'alice', 30, true"
C. "alice; 30; true"
D. "alice, '30', 'true'"

Solution

  1. Step 1: Understand @CsvSource value format

    Values are comma-separated strings; quotes around strings are optional unless spaces or commas inside.
  2. Step 2: Evaluate each option

    "alice, 30, true" correctly uses commas without extra quotes. "'alice', 30, true" adds unnecessary quotes around username. "alice; 30; true" uses semicolons which is invalid. "alice, '30', 'true'" quotes numeric and boolean values unnecessarily.
  3. Final Answer:

    "alice, 30, true" -> Option A
  4. Quick Check:

    Comma-separated values without extra quotes = D [OK]
Hint: Use commas, quote strings only if needed [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Adding quotes around numbers or booleans
  • Quoting strings unnecessarily
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