0
0
JUnittesting~5 mins

@ArgumentsSource with custom providers in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @ArgumentsSource annotation in JUnit?
It allows you to provide custom argument sources for parameterized tests by specifying a class that implements the <code>ArgumentsProvider</code> interface.
Click to reveal answer
beginner
What interface must a custom argument provider implement to be used with @ArgumentsSource?
The custom provider must implement the ArgumentsProvider interface and override the provideArguments(ExtensionContext context) method.
Click to reveal answer
intermediate
How does the provideArguments method work in a custom ArgumentsProvider?
It returns a Stream<Arguments> that supplies the test method with different sets of parameters for each test invocation.
Click to reveal answer
intermediate
Why would you use a custom @ArgumentsSource instead of built-in sources like @ValueSource or @CsvSource?
Custom providers let you generate complex or dynamic test data that built-in sources cannot easily provide, such as data from files, databases, or computed values.
Click to reveal answer
beginner
Show a simple example of a custom <code>ArgumentsProvider</code> class for <code>@ArgumentsSource</code>.
public class SimpleArgumentsProvider implements ArgumentsProvider {
  @Override
  public Stream<Arguments> provideArguments(ExtensionContext context) {
    return Stream.of(
      Arguments.of("apple", 1),
      Arguments.of("banana", 2)
    );
  }
}
Click to reveal answer
Which interface must a class implement to be used with @ArgumentsSource in JUnit?
ATestTemplateInvocationContextProvider
BTestSource
CArgumentsProvider
DParameterResolver
What does the provideArguments method return in a custom ArgumentsProvider?
AStream&lt;Arguments&gt;
BList&lt;String&gt;
CSet&lt;Object&gt;
DMap&lt;String, Object&gt;
Why use a custom @ArgumentsSource instead of @ValueSource?
ATo generate complex or dynamic test data
BBecause <code>@ValueSource</code> is deprecated
CTo provide simple fixed values only
DTo run tests without parameters
Which of these is a valid way to use @ArgumentsSource in a test method?
A@ArgumentsSource("SimpleArgumentsProvider") void test(String fruit) { ... }
B@ArgumentsSource(SimpleArgumentsProvider.class) void test(String fruit, int number) { ... }
C@ArgumentsSource void test(int number) { ... }
D@ArgumentsSource(SimpleArgumentsProvider) void test() { ... }
What happens if your custom ArgumentsProvider returns an empty stream?
AThe test runs once with null arguments
BJUnit throws an exception
CThe test is skipped
DThe test runs zero times (no invocations)
Explain how to create and use a custom argument provider with @ArgumentsSource in JUnit.
Think about the steps to supply custom data to parameterized tests.
You got /5 concepts.
    Describe scenarios where using a custom @ArgumentsSource is better than built-in sources.
    Consider when simple fixed values are not enough.
    You got /4 concepts.