我正在尝试在 junit 5 的帮助下为一个类编写一些测试。我已经使用 Maven 导入了依赖项 但是当我尝试使用注释 @CsvFileSource(resources = "/teSTList.csv") 导入一个 csv 文件以用作测试用例时 我得到这个错误
org.junit.platform.commons.PreconditionViolationException: Classpath resource [/testlist.csv] does not exist
这是我正在运行的代码
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);
}
}
最佳答案
由于 CSV 文件位于 com.faezeh.shayesteh
包中,您必须指定相应的类路径位置:
@CsvFileSource(resources = "/com/faezeh/shayesteh/testlist.csv")
查看target/test-classes/
,这是测试类路径根目录(/
)。如果将 CSV 文件放在 src/test/resources/
中,您将直接在根路径下找到它。这样您就可以坚持使用 resource = "/teSTList.csv"
。
关于java - junit5 无法识别 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61171050/