import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculatorTest {
// Method to test
public int add(int a, int b) {
return a + b;
}
@ParameterizedTest(name = "add({0}, {1}) = {2}")
@CsvSource({
"1, 2, 3",
"5, 7, 12",
"10, 15, 25",
"0, 0, 0",
"-1, 1, 0"
})
void testAdd(int a, int b, int expectedSum) {
assertEquals(expectedSum, add(a, b));
}
}This test class CalculatorTest contains a simple add method to add two integers.
The test method testAdd is annotated with @ParameterizedTest and @CsvSource to provide multiple sets of input data inline as CSV strings.
Each CSV row has three values: the first two are inputs a and b, and the third is the expected sum.
The test runs once for each CSV row, calling add(a, b) and asserting the result equals expectedSum.
This approach keeps the test concise, readable, and easy to add more test cases by just adding more CSV lines.