Iterating over test inputs inside a @Test method executes all cases in a
single test invocation: the first failure aborts the remaining cases, and test
reports do not identify which input failed.
Using TestParameterInjector with @TestParameter runs each input case in
strict isolation with per-case reporting:
@Test
public void parseValidUnits() {
for (TimeUnit unit : TimeUnit.values()) {
assertThat(parseUnit(unit.name())).isEqualTo(unit);
}
}
@RunWith(TestParameterInjector.class)
public final class MyTest {
@Test
public void parseValidUnits(@TestParameter TimeUnit unit) {
assertThat(parseUnit(unit.name())).isEqualTo(unit);
}
}
For explicit subsets of values, pass the elements to @TestParameter:
@Test
public void acceptsKnownSchemes() {
for (String scheme : ImmutableList.of("http", "https")) {
assertThat(isValidScheme(scheme)).isTrue();
}
}
@RunWith(TestParameterInjector.class)
public final class MyTest {
@Test
public void acceptsKnownSchemes(@TestParameter({"http", "https"}) String scheme) {
assertThat(isValidScheme(scheme)).isTrue();
}
}
Suppress false positives by adding the suppression annotation @SuppressWarnings("LoopToTestParameter") to the enclosing element.