junit5 无法识别 csv 文件

Fae*_*teh 2 java junit maven junit5

我正在尝试在 junit 5 的帮助下为一个类编写一些测试。我已经使用 Maven 导入了依赖项,但是当我尝试使用注释 @CsvFileSource(resources = "/testlist) 导入一个 csv 文件以用作测试用例时.csv") 我收到这个错误

org.junit.platform.commons.PreconditionViolationException: Classpath resource [/testlist.csv] does not exist
Run Code Online (Sandbox Code Playgroud)

这是我正在运行的代码

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

    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我的目录的排序方式 在此处输入图片说明

我需要提到当我不使用 Maven 作为框架并对我的目录进行如下排序时它工作正常并且没有问题 在此处输入图片说明

bea*_*u13 5

由于 CSV 文件驻留在com.faezeh.shayesteh包中,您必须指定相应的类路径位置:

@CsvFileSource(resources = "/com/faezeh/shayesteh/testlist.csv")
Run Code Online (Sandbox Code Playgroud)

看看target/test-classes/,这是测试类路径根(/)。如果你把你的 CSV 文件放在里面src/test/resources/,你会直接在根路径下找到它。这样你就可以坚持resource = "/testlist.csv"