在junit5中无法识别csv文件

时间:2020-04-12 11:57:49

标签: java maven junit junit5

我正在尝试借助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);

    }
}

这是我的目录排序的方式 enter image description here

我需要提及的是,当我不使用Maven作为框架并按如下所示对目录进行排序时,它工作正常并且没有问题 enter image description here

3 个答案:

答案 0 :(得分:4)

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

@CsvFileSource(resources = "/com/faezeh/shayesteh/testlist.csv")

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

答案 1 :(得分:0)

如果您的文件在测试资源文件夹中,请不要忘记添加 [0]

this_file.write(line[0].replace(s,'2'))

改为以下:

/

答案 2 :(得分:0)

在我的情况下 IntelliJ 只是没有足够快地在测试 /resources 中注册新的 csv 文件