Video in progress

Junior 🧪 Testing

What does @ParameterizedTest give you, and which sources do you know for it?

It runs one test method several times with different inputs. The data comes from sources such as @ValueSource, @CsvSource, @MethodSource and @EnumSource.

📖 Answer

Instead of copying the same test for five values:

@ParameterizedTest
@CsvSource({
    "998901234567, true",
    "+998901234567, true",
    "90123, false",
    "'', false"
})
void telefon_raqam_tekshiruvi(String input, boolean expected) {
    assertEquals(expected, PhoneValidator.isValid(input));
}

Each row shows up in the report as a separate test, so it's immediately clear which one failed.

The sources:

  • @ValueSource(ints = {1, 2, 3}) for simple cases with one argument;
  • @CsvSource / @CsvFileSource for several arguments;
  • @EnumSource(Status.class) for every value of an enum;
  • @MethodSource("cases") when you need complex objects: a static method that returns a Stream<Arguments>;
  • @NullSource, @EmptySource for edge cases.

It needs the junit-jupiter-params dependency, but spring-boot-starter-test already brings it in.