Junit 5 Parameterized Tests with examples

By | October 4, 2020

Using Parameterized tests make it possible to run a test multiple times with different arguments. They are declared just like regular @Test methods but use the @ParameterizedTest annotation instead.

Additionally, we must declare at least one source that will provide the arguments for each invocation and then consume the arguments in the test method.

The following example demonstrates a parameterized test that uses the @ValueSource annotation to specify a String array as the source of arguments.

 Sources of Arguments in Parameterized Tests

1.@ValueSource

@ValueSource is one of the simplest possible sources. We can use this annotation to specify a single array of literal values and can only be used for providing a single argument per parameterized test invocation.

2.Null and Empty Sources

@NullSource

It provides a single null argument to the annotated @ParameterizedTest method. @NullSource cannot be used for a parameter that has a primitive type.

@EmptySource

It provides a single empty argument to the annotated @ParameterizedTest method for parameters of the following types: java.lang.String, java.util.List, java.util.Set, java.util.Map, primitive arrays (e.g., int[], char[][], etc.), object arrays (e.g.,String[], Integer[][], etc.).

                Subtypes of the supported types are not supported.

@NullAndEmptySource

It is composed of annotation that combines the functionality of @NullSource and @EmptySource.

  If you need to supply multiple  blank strings to a parameterized test,you can achieve that using @ValueSource — for example, @ValueSource(strings = {” “, ”   “, “\t”, “\n”}).

3.@EnumSource

@EnumSource provides a convenient way to use Enum constants.

The annotation’s value attribute is optional. When it is omitted then the declared type of the first method parameter is used. The test will be failed if it does not reference an enum type. so, the value attribute is required in the above example because the method parameter is declared as TemporalUnit. To change the method parameter type to ChronoUnit allows you to omit the explicit enum type from the annotation as follows.

he annotation provides an optional names attribute that lets you specify which constants shall be used, like in the following example. If omitted, all constants will be used.

The @EnumSource annotation also provides an optional mode attribute that enables fine-grained control over which constants are passed to the test method. For example, We can exclude names from the enum constant pool or specify regular expressions as in the following examples.

 

4.@MethodSource

@MethodSource allows you to refer to one or more factory methods of the test

Factory methods within the test class must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS); whereas, factory methods in external classes must always be static. Additionally, such factory methods should not accept any arguments. class or external classes.

5.@CsvSource

@CsvSource allows you to express argument lists as comma-separated values (i.e., String literals).

The default delimiter is a comma (,), but you can use another character by setting the delimiter attribute. Alternatively, the delimiter string attribute allows us to use a String delimiter instead of a single character. However, both delimiter attributes cannot be set simultaneously.

6.@CsvFileSource

@CsvFileSource lets you use CSV files from the classpath. Here each line from a CSV file results in one invocation of the parameterized test.

7.@ArgumentsSource

@ArgumentsSource can be used to specify a custom, reusable ArgumentsProvider. You should note that an implementation of ArgumentsProvider interface must be declared as either a top-level class or as a static nested class.

Let’s try to understand the above concept using a demo project

 

pom.xml

myfile.csv

 

MyUtils.java

SourceProviders.java

NAMES.java 

MyUtilsTest.java

The output of the above project:

 

You May Also Like:

Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5

That’s all about Junit 5 Parameterized Tests with examples
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *