What if you could test dozens of cases with just one simple test method?
Why @ParameterizedTest annotation in JUnit? - Purpose & Use Cases
Imagine you have a calculator app and want to test its addition function with many pairs of numbers. You write a separate test for each pair manually.
Writing many similar tests one by one is slow and boring. It's easy to make mistakes or forget some cases. Running all tests takes longer and the code becomes messy.
The @ParameterizedTest annotation lets you run the same test logic with different inputs automatically. You write the test once and provide many data sets. This saves time and keeps tests clean.
void testAdd1() { assertEquals(3, add(1, 2)); }
void testAdd2() { assertEquals(5, add(2, 3)); }@ParameterizedTest
@CsvSource({"1, 2, 3", "2, 3, 5"})
void testAdd(int a, int b, int expected) { assertEquals(expected, add(a, b)); }You can easily test many input cases with less code, making your tests faster, clearer, and less error-prone.
Testing a login form with many username and password combinations to ensure all valid and invalid inputs behave correctly.
Manual repetitive tests are slow and error-prone.
@ParameterizedTest runs one test with many inputs automatically.
This makes testing faster, cleaner, and more reliable.