Code examples for - Parameterized tests with JUnit 5
This commit is contained in:
@@ -11,18 +11,17 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
public class AssertJTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("checkNumber_args")
|
||||
@MethodSource("checkNumberArgs")
|
||||
void checkNumber(int number, Consumer<Integer> consumer) {
|
||||
|
||||
consumer.accept(number);
|
||||
}
|
||||
|
||||
static Stream<Arguments> checkNumber_args() {
|
||||
static Stream<Arguments> checkNumberArgs() {
|
||||
|
||||
Consumer<Integer> evenConsumer = i -> Assertions.assertThat(i % 2).isZero();
|
||||
Consumer<Integer> oddConsumer = i -> Assertions.assertThat(i % 2).isEqualTo(1);
|
||||
|
||||
return Stream.of(Arguments.of(2, evenConsumer),
|
||||
Arguments.of(3, oddConsumer));
|
||||
return Stream.of(Arguments.of(2, evenConsumer), Arguments.of(3, oddConsumer));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,15 @@ public class CsvFileSourceTest {
|
||||
void checkCsvFileSource(int number, String expected) {
|
||||
assertEquals(StringUtils.equals(expected, "even") ? 0 : 1, number % 2);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvFileSource(files = "src/test/resources/csv-file-source_attributes.csv",
|
||||
delimiterString = "|",
|
||||
lineSeparator = "||",
|
||||
numLinesToSkip = 1)
|
||||
void checkCsvFileSourceAttributes(int number, String expected) {
|
||||
assertEquals(StringUtils.equals(expected, "even") ? 0 : 1, number % 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.util.stream.Stream;
|
||||
|
||||
public class ExternalMethodSource {
|
||||
|
||||
static Stream<String> checkExternalMethodSource_args() {
|
||||
static Stream<String> checkExternalMethodSourceArgs() {
|
||||
return Stream.of("a1", "b2");
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ public class ExternalMethodSourceTest {
|
||||
|
||||
// Note: The test will try to load the external method
|
||||
@ParameterizedTest
|
||||
@MethodSource("source.method.ExternalMethodSource#checkExternalMethodSource_args")
|
||||
@MethodSource("source.method.ExternalMethodSource#checkExternalMethodSourceArgs")
|
||||
void checkExternalMethodSource(String word) {
|
||||
assertTrue(StringUtils.isAlphanumeric(word), "Supplied word is not alpha-numeric");
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@ public class MethodSourceTest {
|
||||
|
||||
// Note: The test will try to load the supplied method
|
||||
@ParameterizedTest
|
||||
@MethodSource("checkExplicitMethodSource_args")
|
||||
@MethodSource("checkExplicitMethodSourceArgs")
|
||||
void checkExplicitMethodSource(String word) {
|
||||
assertTrue(StringUtils.isAlphanumeric(word), "Supplied word is not alpha-numeric");
|
||||
}
|
||||
|
||||
static Stream<String> checkExplicitMethodSource_args() {
|
||||
static Stream<String> checkExplicitMethodSourceArgs() {
|
||||
return Stream.of("a1", "b2");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
|| NUMBER | ODD_EVEN ||
|
||||
|| 2 | even ||
|
||||
|| 3 | odd ||
|
||||
|
Reference in New Issue
Block a user