Dependency Injection and Testing in JUnit 5. Part 3
3. Parameterized tests
Parameterized tests make possible the running of a test multiple times with different arguments. The great benefit is that you can write one single test, and the testing is done on a series of different arguments. Your tests will be safer and check various input data. The methods are annotated using @ParameterizedTest. You must declare at least one source, which provides the arguments for each invocation and then consumes the arguments in the test method.
@ValueSource is the simplest of the possible sources. You must specify a single array of literal values for providing a single argument per parameterized test invocation. The usage of this annotation is shown into listing 4. The scenario of the test intends to check the number of words in some given sentences, provided as parameters.
Into the previous example, we do the following:
- We mark the test as a parameterized one using the corresponding annotation (1).
- Then, we specify the values to be passed as argument of the testing method (2). The testing method is executed twice, once for each of the arguments provided by the @ValueSource annotation.
@EnumSource provides the possibility to use enum instances. The annotation provides an optional names parameter which allows you to specify which of the instances must be used or excluded. By default, all instances of an enum are used.
Listing 5 shows the usage of the @EnumSource annotation to check the number of words in some given sentences, provided as enum instances.
Into the previous example we’ have one parameterized test. The functionality is as follows:
- The test receives as parameters a CSV as specified into the @CsvSource annotation (1). This test is executed three times, once for each of the CSV lines.
- The CSV line is parsed and the first value is assigned to the expected parameter, and the second value is assigned to the sentence parameter.
@CsvFileSource allows you to use CSV files from the classpath. Each line from a CSV file triggers one execution of the parameterized test.
Listing 6 shows the usage of the @CsvFileSource annotation, and listing 7 displays the content of the word_counter.csv file on the classpath. The scenario of the test intends to check the number of words in some given sentences, provided as parameters—this time, in CSV format, having a CSV file as resource input.
Into the previous example we’ve one parameterized test. It receives as parameters the ones specified into the file indicated into the @CsvFileSource annotation (1). This test is executed three times, once for each of the CSV file lines. The CSV file line is parsed and the first value is assigned to the expected parameter, and the second value is assigned to the sentence parameter.
The article discussed the new possibilities to provide parameters to JUnit 5 unit tests, through dependency injection. We presented how this is effectively done with the help of different parameter resolvers (TestInfoParameterResolver, TestReporterParameterResolver, RepetitionInfoParameterResolver). We also showed how to write repeated and parameterized tests (annotations @RepeatedTest and @ParameterizedTest).
Interested in Java? Check out our trainings.
Java and Web Technologies Expert