Issue
I'm trying write some tests for a class with the help of junit 5.I have imported the dependencies using Maven but when I'm try to import a csv file to use as test case using annotation @CsvFileSource(resources = "/testlist.csv") I get this error
org.junit.platform.commons.PreconditionViolationException: Classpath resource [/testlist.csv] does not exist
this is the code I'm running
package com.faezeh.shayesteh;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
public class MultipleOperationParamTest {
@ParameterizedTest
@CsvFileSource(resources = "/testlist.csv")
void testMultipleOpWithCsvFileSrc(int operand, int data, int result){
MultiplyOperation multop = new MultiplyOperation(operand);
int actual = multop.operate(data);
Assertions.assertEquals(result,actual);
}
}
and this is how my directories are sorted
I need to mention when I'm not using Maven as framework and sort my directories as below it works fine and there's no problem
Solution
Since the CSV file resides within the com.faezeh.shayesteh
package, you have to specify the corresponding classpath location:
@CsvFileSource(resources = "/com/faezeh/shayesteh/testlist.csv")
Have a look at target/test-classes/
, this is the test classpath root (/
). If you put your CSV file inside src/test/resources/
, you will find it directly under the root path. This way you could stick to resource = "/testlist.csv"
.
Answered By - beatngu13