|
| 1 | +package org.baeldung.java.parameterisedsource; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.EnumSet; |
| 7 | +import java.util.stream.Stream; |
| 8 | + |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.CsvSource; |
| 11 | +import org.junit.jupiter.params.provider.EnumSource; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
| 13 | +import org.junit.jupiter.params.provider.ValueSource; |
| 14 | + |
| 15 | +public class ParameterizedUnitTest { |
| 16 | + |
| 17 | + @ParameterizedTest |
| 18 | + @ValueSource(strings = { "Hello", "World" }) |
| 19 | + void givenString_TestNullOrNot(String word) { |
| 20 | + assertNotNull(word); |
| 21 | + } |
| 22 | + |
| 23 | + @ParameterizedTest |
| 24 | + @EnumSource(value = PizzaDeliveryStrategy.class, names = {"EXPRESS", "NORMAL"}) |
| 25 | + void givenEnum_TestContainsOrNot(PizzaDeliveryStrategy timeUnit) { |
| 26 | + assertTrue(EnumSet.of(PizzaDeliveryStrategy.EXPRESS, PizzaDeliveryStrategy.NORMAL).contains(timeUnit)); |
| 27 | + } |
| 28 | + |
| 29 | + @ParameterizedTest |
| 30 | + @MethodSource("wordDataProvider") |
| 31 | + void givenMethodSource_TestInputStream(String argument) { |
| 32 | + assertNotNull(argument); |
| 33 | + } |
| 34 | + |
| 35 | + static Stream<String> wordDataProvider() { |
| 36 | + return Stream.of("foo", "bar"); |
| 37 | + } |
| 38 | + |
| 39 | + @ParameterizedTest |
| 40 | + @CsvSource({ "1, Car", "2, House", "3, Train" }) |
| 41 | + void givenCSVSource_TestContent(int id, String word) { |
| 42 | + assertNotNull(id); |
| 43 | + assertNotNull(word); |
| 44 | + } |
| 45 | +} |
0 commit comments