0
0
JUnittesting~15 mins

@ArgumentsSource with custom providers in JUnit - Build an Automation Script

Choose your learning style9 modes available
Automate parameterized test using @ArgumentsSource with a custom provider
Preconditions (2)
Step 1: Create a test class with a parameterized test method
Step 2: Annotate the test method with @ParameterizedTest and @ArgumentsSource referencing the custom provider
Step 3: Implement the custom ArgumentsProvider to supply multiple sets of arguments
Step 4: Run the test and verify that the test method executes once per argument set
✅ Expected Result: The test method runs multiple times with different arguments supplied by the custom provider, and all assertions pass.
Automation Requirements - JUnit 5
Assertions Needed:
Verify the test method receives correct arguments from the custom provider
Verify the test method executes the expected number of times
Best Practices:
Use @ParameterizedTest with @ArgumentsSource for custom argument providers
Implement ArgumentsProvider interface correctly
Use Stream.of or Arguments.of to supply arguments
Keep test method simple and focused on assertions
Avoid hardcoding test data inside the test method
Automated Solution
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import java.util.stream.Stream;

public class CustomArgumentsSourceTest {

    static class MyArgumentsProvider implements ArgumentsProvider {
        @Override
        public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
            return Stream.of(
                Arguments.of("apple", 5),
                Arguments.of("banana", 6),
                Arguments.of("cherry", 6)
            );
        }
    }

    @ParameterizedTest
    @ArgumentsSource(MyArgumentsProvider.class)
    void testWithCustomArgumentsSource(String fruit, int length) {
        Assertions.assertEquals(length, fruit.length());
    }
}

The code defines a test class CustomArgumentsSourceTest with a nested static class MyArgumentsProvider that implements ArgumentsProvider. This provider returns a stream of argument sets, each containing a fruit name and its expected length.

The test method testWithCustomArgumentsSource is annotated with @ParameterizedTest and @ArgumentsSource(MyArgumentsProvider.class). This tells JUnit to run the test multiple times, once for each argument set provided by MyArgumentsProvider.

Inside the test method, an assertion checks that the length of the fruit string matches the expected length. This verifies that the arguments are passed correctly.

This approach keeps test data separate from the test logic, making tests clean and maintainable.

Common Mistakes - 4 Pitfalls
Not implementing the ArgumentsProvider interface correctly
Hardcoding test data inside the test method instead of using the provider
Using incorrect annotations like @ValueSource instead of @ArgumentsSource for custom providers
Not matching the test method parameters with the arguments provided
Bonus Challenge

Now add data-driven testing with 3 different sets of fruit names and their lengths using the custom ArgumentsProvider

Show Hint