Bird
Raised Fist0
JUnittesting~20 mins

Custom argument providers in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Argument Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a JUnit test using a custom argument provider
Given the following JUnit 5 test code using a custom argument provider, what will be the output when the test runs?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CustomArgProviderTest {
    static class EvenNumberProvider implements ArgumentsProvider {
        @Override
        public Stream<Arguments> provideArguments(org.junit.jupiter.api.extension.ExtensionContext context) {
            return Stream.of(2, 4, 6, 8).map(Arguments::of);
        }
    }

    @ParameterizedTest
    @ArgumentsSource(EvenNumberProvider.class)
    void testEvenNumbers(int number) {
        assertTrue(number % 2 == 0);
    }
}
AAll tests pass successfully
BTest fails because the argument provider returns odd numbers
CTest fails due to a runtime exception in the argument provider
DTest fails because the test method signature is incorrect
Attempts:
2 left
💡 Hint
Check what numbers the custom argument provider returns and what the assertion checks.
❓ assertion
intermediate
2:00remaining
Assertion outcome with a custom argument provider
Consider a custom argument provider that supplies the following integers: 1, 3, 5, 7. The test method asserts that the number is less than 5. What will be the assertion outcomes for each input?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LessThanFiveProviderTest {
    static class OddNumberProvider implements ArgumentsProvider {
        @Override
        public Stream<Arguments> provideArguments(org.junit.jupiter.api.extension.ExtensionContext context) {
            return Stream.of(1, 3, 5, 7).map(Arguments::of);
        }
    }

    @ParameterizedTest
    @ArgumentsSource(OddNumberProvider.class)
    void testLessThanFive(int number) {
        assertTrue(number < 5);
    }
}
ATests for 1, 3, and 5 pass; test for 7 fails
BTests for 1 and 3 pass; tests for 5 and 7 fail
CAll tests fail
DAll tests pass
Attempts:
2 left
💡 Hint
Check which numbers satisfy the condition number < 5.
🔧 Debug
advanced
2:00remaining
Identify the error in a custom argument provider implementation
What is the cause of the runtime error in the following custom argument provider code?
JUnit
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.stream.Stream;

public class NullProvider implements ArgumentsProvider {
    @Override
    public Stream<Arguments> provideArguments(org.junit.jupiter.api.extension.ExtensionContext context) {
        return null;
    }
}
AThe provideArguments method returns null instead of a Stream, causing a NullPointerException
BThe class does not implement ArgumentsProvider correctly
CThe method signature is incorrect
DThe Stream returned contains null elements
Attempts:
2 left
💡 Hint
Check what the provideArguments method returns and what JUnit expects.
❓ framework
advanced
2:00remaining
Correct usage of custom argument provider in JUnit 5
Which of the following is the correct way to use a custom argument provider in a JUnit 5 parameterized test?
A
@ParameterizedTest
@CsvSource({"a", "b"})
void testMethod(String input) { /* test code */ }
B
@Test
@ArgumentsSource(MyProvider.class)
void testMethod(String input) { /* test code */ }
C
@ParameterizedTest
@ValueSource(strings = {"a", "b"})
void testMethod(String input) { /* test code */ }
D
@ParameterizedTest
@ArgumentsSource(MyProvider.class)
void testMethod(String input) { /* test code */ }
Attempts:
2 left
💡 Hint
Custom argument providers require a specific annotation and test type.
🧠 Conceptual
expert
3:00remaining
Understanding lifecycle and state in custom argument providers
A custom argument provider maintains internal state by storing a counter that increments each time provideArguments is called. What is the expected behavior of this counter across multiple test method invocations in JUnit 5?
AThe argument provider is a singleton and the counter is shared across all test classes
BThe same instance is reused for all test methods, so the counter accumulates across all tests
CA new instance of the argument provider is created for each test method invocation, so the counter resets each time
DJUnit 5 does not allow stateful argument providers and throws an error
Attempts:
2 left
💡 Hint
Consider how JUnit 5 manages instances of argument providers during test execution.

Practice

(1/5)
1. What is the main purpose of a custom argument provider in JUnit?
easy
A. To generate test reports
B. To create new test methods automatically
C. To supply test data to parameterized tests in a custom way
D. To replace the @Test annotation

Solution

  1. Step 1: Understand the role of argument providers

    Argument providers supply data to parameterized tests, allowing tests to run multiple times with different inputs.
  2. Step 2: Identify the purpose of custom argument providers

    Custom argument providers let you define your own way to supply test data beyond built-in sources.
  3. Final Answer:

    To supply test data to parameterized tests in a custom way -> Option C
  4. Quick Check:

    Custom argument providers = supply test data [OK]
Hint: Custom providers supply test data uniquely for parameterized tests [OK]
Common Mistakes:
  • Confusing argument providers with test method creators
  • Thinking they replace @Test annotation
  • Assuming they generate reports
2. Which interface must you implement to create a custom argument provider in JUnit?
easy
A. ArgumentsSource
B. ArgumentsProvider
C. TestTemplateInvocationContextProvider
D. TestInstancePostProcessor

Solution

  1. Step 1: Recall JUnit interfaces for argument providers

    The interface to implement for custom argument providers is ArgumentsProvider.
  2. Step 2: Differentiate from other interfaces

    ArgumentsSource is an annotation, not an interface. The others serve different purposes.
  3. Final Answer:

    ArgumentsProvider -> Option B
  4. Quick Check:

    Implement ArgumentsProvider for custom data [OK]
Hint: Implement ArgumentsProvider interface for custom data supply [OK]
Common Mistakes:
  • Confusing ArgumentsSource annotation with interface
  • Choosing unrelated interfaces
  • Not overriding provideArguments method
3. Given this custom argument provider code snippet, what will be the output when the parameterized test runs?
public class MyProvider implements ArgumentsProvider {
    @Override
    public Stream<Arguments> provideArguments(ExtensionContext context) {
        return Stream.of(Arguments.of("apple"), Arguments.of("banana"));
    }
}

@ParameterizedTest
@ArgumentsSource(MyProvider.class)
void testFruits(String fruit) {
    System.out.println(fruit);
}
medium
A. apple\nbanana (each on separate lines)
B. apple banana
C. Compilation error due to missing imports
D. Runtime error due to wrong method signature

Solution

  1. Step 1: Analyze the provideArguments method

    The method returns a stream with two Arguments: "apple" and "banana".
  2. Step 2: Understand parameterized test execution

    The test runs twice, once with "apple" and once with "banana", printing each on its own line.
  3. Final Answer:

    apple\nbanana (each on separate lines) -> Option A
  4. Quick Check:

    Stream of Arguments runs test twice [OK]
Hint: Each argument runs test once, prints on separate lines [OK]
Common Mistakes:
  • Assuming output is on one line
  • Thinking code has syntax errors without imports
  • Confusing method signature correctness
4. Identify the error in this custom argument provider implementation:
public class BadProvider implements ArgumentsProvider {
    public Stream<Arguments> provideArguments() {
        return Stream.of(Arguments.of(1), Arguments.of(2));
    }
}
medium
A. Method provideArguments must have ExtensionContext parameter
B. Missing @Override annotation
C. Arguments.of() cannot take integers
D. Class must extend ArgumentsSource

Solution

  1. Step 1: Check method signature requirements

    The provideArguments method must override the interface method and accept ExtensionContext as a parameter.
  2. Step 2: Identify the missing parameter

    The code's provideArguments method lacks the ExtensionContext parameter, causing a signature mismatch.
  3. Final Answer:

    Method provideArguments must have ExtensionContext parameter -> Option A
  4. Quick Check:

    Correct method signature requires ExtensionContext [OK]
Hint: provideArguments must accept ExtensionContext parameter [OK]
Common Mistakes:
  • Ignoring method signature parameters
  • Thinking @Override is mandatory but not fixing signature
  • Confusing Arguments.of() usage
5. You want to create a custom argument provider that supplies pairs of integers where the second is always double the first. Which implementation correctly achieves this?
public class DoubleProvider implements ArgumentsProvider {
    @Override
    public Stream<Arguments> provideArguments(ExtensionContext context) {
        // Fill in the blank
    }
}
hard
A. return Stream.of(Arguments.of(1, 3), Arguments.of(2, 5), Arguments.of(3, 7));
B. return Stream.of(Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(3, 3));
C. return Stream.empty();
D. return Stream.of(Arguments.of(1, 2), Arguments.of(2, 4), Arguments.of(3, 6));

Solution

  1. Step 1: Understand the requirement

    Each pair must have the second integer double the first.
  2. Step 2: Evaluate each option

    return Stream.of(Arguments.of(1, 2), Arguments.of(2, 4), Arguments.of(3, 6)); correctly pairs (1,2), (2,4), (3,6). Other options provide pairs like (1,3),(2,5),(3,7) or equal numbers (1,1),(2,2),(3,3), or an empty stream.
  3. Final Answer:

    return Stream.of(Arguments.of(1, 2), Arguments.of(2, 4), Arguments.of(3, 6)); -> Option D
  4. Quick Check:

    Second integer = 2 * first integer [OK]
Hint: Check if second number is double the first in pairs [OK]
Common Mistakes:
  • Providing pairs where second is not double
  • Returning empty stream by mistake
  • Confusing argument order
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