Parameterized Tests with @CsvSource and @MethodSource
Run the same test with multiple input sets using @ParameterizedTest, @CsvSource, and @MethodSource.
Why Parameterized Tests?
Running the same test logic with multiple input sets reveals edge cases without copy-pasting test methods. JUnit 5 supports parameterized tests natively with @ParameterizedTest.
@ValueSource: Simple Single-Value Tests
@ValueSource provides a single parameter of a given type. The test runs once per value.
@ParameterizedTest
@ValueSource(strings = {"", " ", "\t"})
void blank_strings_are_rejected(String input) {
assertThrows(IllegalArgumentException.class, () -> validator.validate(input));
}All lessons in this course
- Parameterized Tests with @CsvSource and @MethodSource
- Mockito Advanced: Argument Captors and Spies
- Spring Boot Test Slices: @WebMvcTest and @DataJpaTest
- Testcontainers: Real Database Integration Tests