JUnit 5 Dynamic file path for @CsvFileSource

44 views Asked by At

I use the below to access a csv file in my paramaterized JUnit5 test but is there any way to dynamically provide this csv file or a different file path to the location of the csv file?

@CsvFileSource(resources = "/testDataFile/testData.csv" , numLinesToSkip = 1, delimiter =  ';', encoding = "UTF-8")

In other words can I dynamically update "/testDataFile/testData.csv"?

I want a way to dynamically pass in the file path for my csv file so it is not a hardcoded location or file name.

1

There are 1 answers

0
beatngu13 On

I'm afraid this is not possible. In general, the value for an annotation must be a constant expression. And the @CsvFileSource API doesn't support dynamic/programmatic control over the supplied value(s), at least to my knowledge.

Two possible custom solutions:

  1. Create an @ArgumentsSource and a corresponding ArgumentsProvider, based on CsvArgumentsProvider (which is non-public).

  2. Create an extension that dynamically creates the CSV file(s) specified as resources/files in @CsvFileSource.