@CsvSource and @CsvFileSource

Another way to specify the source of arguments for parameterized tests is using comma-separated values (CSV). This can be done using the annotation @CsvSource, which allows to embed CSV content as String in the value of the annotation.

Consider the following example. It contains a Jupiter parameterized test (named testWithCsvSource), which is using the annotation @CsvSource. This annotation contains an array of Strings. In each element of the array, we can see there is a different value separated by commas.

The content of the CSV is automatically converted to String and int. To find out more about the implicit type conversion made in parameters by JUnit 5, take a look to the section Argument conversion in this chapter.
package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class CsvSourceParameterizedTest {

@ParameterizedTest
@CsvSource({ "hello, 1", "world, 2", "'happy, testing', 3" })
void testWithCsvSource(String first, int second) {
System.out.println("Parameterized test with (String) " + first
+ " and (int) " + second);
assertNotNull(first);
assertNotEquals(0, second);
}

}

All in all, when executing this test class, there will be three single tests, each per entry in the array. Each execution will be invoked, passing two arguments to the test. The first one is named first and its type is String, and second one is called second and its type is int.

Execution of parameterized test using @CsvSource

If the amount of CSV data is big, it might be more convenient using the annotation @CsvFileSource instead. This annotation allows to feed the parameterized test with a CSV file located in the classpath of the project. In the following example, we use the file input.csv:

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

class CsvFileSourceParameterizedTest {

@ParameterizedTest
@CsvFileSource(resources = "/input.csv")
void testWithCsvFileSource(String first, int second) {
System.out.println("Yet another parameterized test with
(String) " + first + " and (int) " + second);
assertNotNull(first);
assertNotEquals(0, second);
}

}

Internally, the annotation @CsvFileSource locates the file using the method getResourceAsStream() of the standard Java class java.lang.Class. Therefore, the path of the file is interpreted as a path local to the package class we are calling it from. Since our resource is located in the root of the classpath (in the example it is located in the folder src/test/resources), we need to locate it as /input.csv.

Location and content of input.csv in the example with @CsvFileSource

The following screenshot shows the output of the test when it is executed with Maven. Since the CSV has three rows of data, there are three test executions, each one with two parameters (first one as String and second one as int):

Execution of parameterized test using @CsvFileSource
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.37.250