【问题标题】:csv file is not recognized in junit5csv文件在junit5中无法识别
【发布时间】:2020-04-12 11:57:49
【问题描述】:

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

    }
}

这就是我的目录的排序方式

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

【问题讨论】:

    标签: java maven junit junit5


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      @CsvFileSource(resources = "testlist.csv")  
      

      更改如下:

      @CsvFileSource(resources = "/testlist.csv")
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-26
          • 1970-01-01
          • 1970-01-01
          • 2014-04-25
          • 2018-02-15
          • 1970-01-01
          • 2021-11-22
          • 1970-01-01
          相关资源
          最近更新 更多