Faezeh Shayesteh
Faezeh Shayesteh

Reputation: 97

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

Upvotes: 4

Views: 5226

Answers (3)

electrobabe
electrobabe

Reputation: 1647

In my case IntelliJ just didn't register the new csv file in the test /resources fast enough

Upvotes: 0

Viktor Reinok
Viktor Reinok

Reputation: 130

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

Upvotes: 2

beatngu13
beatngu13

Reputation: 9393

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

Upvotes: 8

Related Questions