简体   繁体   中英

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

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?

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, eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM