JUnit5: Where is the root of @CsvFileSource defined and can that definition be changed to refer to a different directory?

huangapple go评论81阅读模式
英文:

JUnit5: Where is the root of @CsvFileSource defined and can that definition be changed to refer to a different directory?

问题

我从maven-quickstart开始一个项目,并添加了JUNIT5。
任何地方都没有 'resources' 文件夹。它使用 pom.xml 构建,直到 "package" 目标为止。

之后的一段时间里,测试人员想要添加一个从 CSV 文件中读取的测试。
他在设置时遇到了一些问题,我从记忆中想起它会在 test/resources 中查找。

现在我们都没问题了,但我就是停不下来思考:'test/resources' 是不是硬编码到 JUnit 中?还是它是从项目原型中某种方式衍生出来的?

在项目设置、虚拟机设置或者测试方法中是否有办法编辑这个引用?

英文:

I started a project as a maven-quickstart and added JUNIT5.
There was no 'resources' folder anywhere. It builds using a pom.xml and up to "package" goal.

Some time after, one of the testers wanted to add a test that reads from a CSV file.
He had some trouble setting up and I recalled from just memory that it will look in test/resources.

We are all fine now but I just can't stop wondering: Is 'test/resources' hard-coded into JUnit? Or is it somehow derived from the project archetype?

Is there a way to edit this reference in the project settings, vm settings or maybe in the very test method?

答案1

得分: 2

来源:https://junit.org/junit5/docs/5.3.0/api/org/junit/jupiter/params/provider/CsvFileSource.html

> 用于从一个或多个类路径资源加载逗号分隔值(CSV)文件。

使用此注释加载资源的方式(如果它们不在类路径上)是将它们放在类路径上。

因此,回答你的问题 - 是的,你可以通过更改类路径来更改此设置。

如何做到这一点取决于你用来构建的工具,例如:

对于Java,你使用 java -cp ...;Maven、Gradle、Ant 都有不同的方式,因为你没有提供一个具体的问题,所以我们不能给出具体的答案。

编辑 - 由于你在使用Maven - 创建一个 src/test/resources(或 src/main/resources)目录,除非你覆盖了Maven的默认设置,它们会自动成为测试类路径的一部分(分别成为类路径的一部分)。将你的文件放在 src/test/resources 目录中,应该就可以工作了。

英文:

From https://junit.org/junit5/docs/5.3.0/api/org/junit/jupiter/params/provider/CsvFileSource.html

> used to load comma-separated value (CSV) files from one or more classpath resources.

The way to load resources with this annotation (if they are not on the classpath) is to put them on the classpath.

So, to answer your question - yes you can change this setting by changing the classpath.

How you do that depends on what you are using to build, e.g.:

For java, you use java -cp ..., maven, gradle, ant all have different ways and since you have not posted a specific question we can't give a specific answer.

EDIT - Since you're using Maven - create a src/test/resources (or src/main/resources) and unless you're overriding the defaults Maven will automatically make those part of the test classpath (and classpath, respictively). Put your file in src/test/resources and it should work.

huangapple
  • 本文由 发表于 2020年9月29日 19:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64118703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定