JUnit 5 parameterized test example
Sometimes you need to run the same test with various sets of parameters, these are called Parameterized tests
.
The examples below show how to write Parameterized JUnit 5
Tests.
To run Parameterized JUnit 5
Tests, you need to use @ParameterizedTest
annotation, along with
@ValueSource
, @EnumSource
or @CsvSource
annotation.
1) Add below dependencies to pom.xml
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.8.1</version> <scope>test</scope> </dependency>
2) Write Parameterized JUnit 5 test using @ParameterizedTest
& @ValueSource
annotations
@ValueSource
annotation allows to pass values using single dimensional array, whose elements can be used for testing one by one.
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class JUnit5ParameterizedTest { @ParameterizedTest @ValueSource(ints = { 2, 1, 5 }) public void testArraySize(int num) { assertTrue(num > 0); } @ParameterizedTest @ValueSource(strings = { "i love junit5", "test using junit5" }) void testWordsInSentence(String str) { assertNotNull(str); assertEquals(3, str.split(" ").length); } }
2) Write Parameterized JUnit 5 test using @ParameterizedTest
& @EnumSource
annotations
@EnumSource
annotation allows to pass values as Enum.
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource.Mode; public class JUnit5ParameterizedTest { @ParameterizedTest @EnumSource(value = Colors.class) void testAllColorsLength(Colors colors) { assertTrue(colors.value().length() > 0); } @ParameterizedTest @EnumSource(value = Colors.class, names = { "GREEN", "BLUE" }) void testFewColorsLength(Colors colors) { assertTrue(colors.value().length() > 0); } @ParameterizedTest @EnumSource(value = Colors.class, mode = Mode.EXCLUDE, names = { "GREEN", "BLUE" }) void testYellowColorLength(Colors colors) { assertEquals("Yellow", colors.value()); } }
3) Write Parameterized JUnit 5 test using @ParameterizedTest
& @CsvSource
annotations
@CsvSource
annotation allows to pass values seperated by comma.
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class JUnit5ParameterizedTest { @ParameterizedTest @CsvSource({ "3, i love junit5", "2, junit5 tests" }) void testWordsInASentence(int expected, String str) { assertNotNull(str); assertEquals(expected, str.split(" ").length); } }
4) Write Parameterized JUnit 5 test using @ParameterizedTest
& @CsvFileSource
annotations
@CsvFileSource
annotation allows to pass values using csv file. You can keep test.csv file under /test/resource folder.
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; public class JUnit5ParameterizedTest { @ParameterizedTest @CsvFileSource(resources = "/test.csv") void testNumberOfWordsInASentence(int expected, String str) { assertNotNull(str); assertEquals(expected, str.split(" ").length); } }
References :
ParameterizedTest (JUnit 5.3.0 API)