Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csv file is not recognized in junit5

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 enter image description here

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 enter image description here

like image 998
Faezeh Shayesteh Avatar asked Oct 17 '25 14:10

Faezeh Shayesteh


2 Answers

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".

like image 110
beatngu13 Avatar answered Oct 20 '25 02:10

beatngu13


In case your file is in the test resource folder do not forget to add /

@CsvFileSource(resources = "testlist.csv")  

Change to following:

@CsvFileSource(resources = "/testlist.csv")
like image 36
Viktor Reinok Avatar answered Oct 20 '25 02:10

Viktor Reinok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!