JUNIT / NOTES</>↗JUnit11 Jul 2026
@EnumSource is a JUnit 5 parameterized-test source that runs the same test once for each selected enum constant. Basic usage import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; import static org.junit.jupiter.api.Assertions.assertNotNull; class DirectionTest { enum Direction { NORTH, SOUTH, EAST, WEST } @ParameterizedTest @EnumSource(Direction.class) void shouldTestAllDirections(Direction direction) { assertNotNull(direction...
JUNIT / NOTES</>↗JUnit11 Jul 2026
In JUnit 5, @MethodSource lets you supply test arguments from one or more factory methods. It is commonly used with @ParameterizedTest when your test data is too complex for @ValueSource or @CsvSource. Basic example import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertTrue; class StringTest { @ParameterizedTest @MethodSource("blankStrings")...
JUNIT / NOTES</>↗JUnit11 Jul 2026
Use JUnit 5’s @CsvFileSource with a parameterized test to load rows from a CSV file and pass each row into your test method. 1. Add the CSV file Place the CSV file under src/test/resources, for example: src/test/resources/test-data/users.csv Example CSV: username,age,active alice,30,true bob,25,false charlie,40,true 2. Use @CsvFileSource import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import static org.junit.jupiter.a...
JUNIT / NOTES</>↗JUnit11 Jul 2026
To use @CsvSource in JUnit 5 parameterized tests, you define multiple sets of comma-separated input values directly inside the annotation. Each CSV row becomes one test invocation. 1. Add the Required Imports import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; @CsvSource is part of JUnit Jupiter Params, so make sure your project includes the parameterized test […]
JUNIT / NOTES</>↗JUnit11 Jul 2026
In JUnit 5, @ValueSource is used with @ParameterizedTest to run the same test multiple times with different simple literal values. Basic example import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertTrue; class StringTest { @ParameterizedTest @ValueSource(strings = {"racecar", "radar", "level"}) void palindromeWordsHaveLengthGreaterThanZero(String word) { assertTrue(word.le...
JUNIT / NOTES</>↗JUnit11 Jul 2026
In JUnit 5, parameterized tests let you run the same test multiple times with different input values. You use: @ParameterizedTest instead of: @Test Then you provide test data using a source annotation such as @ValueSource, @CsvSource, @MethodSource, or @EnumSource. Basic Example with @ValueSource import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertTrue; class NumberTest {...