79

In a unit test I need to import a csv file. This is located in the resources folder, i.e. src/test/resources

simpatico
  • 10,709
  • 20
  • 81
  • 126
  • 1
    Found simple solution (Java7+) here : https://stackoverflow.com/a/42375654/1579667 – Benj Jun 13 '18 at 12:48

6 Answers6

88

Probably just useful if you have the file available, for example when doing unit tests - this will not load it out of a jar AFAIK.

URL url = Thread.currentThread().getContextClassLoader().getResource("mypackage/YourFile.csv");
File file = new File(url.getPath());
// where the file is in the classpath eg. <project>/src/test/resources/mypackage/YourFile.csv
AmanicA
  • 4,659
  • 1
  • 34
  • 49
  • 2
    what I was really looking for was abstraction from providing the path to the file, as in getResourceAsStream. Otherwise new File/getFile is more straightforward – simpatico Apr 04 '11 at 21:03
  • 2
    url is null for me with that statement – hithwen Feb 06 '13 at 12:59
  • @hithwen ok fixed my answer, the path you specify is actually relative to the classpath, so you should leave out the `src/test/resources` part. (getResource returns null if it can't find the file) – AmanicA Feb 06 '13 at 22:21
  • 1
    the resolution of the file ends up including the package of the current class. @hithwen – monksy Jul 15 '14 at 19:57
  • 1
    Need to use `new File(url.toURI());` to correctly handle spaces in file path. – kolobok Apr 07 '21 at 08:57
24

You can access test resources using the current thread's classloader:

InputStream stream = Thread.currentThread().getContextClassLoader()
    .getResourceAsStream("YOURFILE.CSV");
Russ Hayward
  • 5,617
  • 2
  • 25
  • 30
  • 2
    but I want a java.io.File and not an InputStream. – simpatico Apr 03 '11 at 12:54
  • then use getClass().getResource("/src/test/resources/YourFile.csv"); http://download.oracle.com/javase/6/docs/api/java/lang/Class.html – Sean Apr 03 '11 at 13:43
  • 1
    so, FileUtils.toFile(Thread.currentThread().getClass().getResource("/src/test/resources/YourFile.csv")); – simpatico Apr 04 '11 at 07:10
14

with guava

import com.google.common.io.Resources;
URL url = Resources.getResource("YourFile.csv");
David Michael Gang
  • 7,107
  • 8
  • 53
  • 98
  • 1
    as @simpatico wants a `java.io.File`, this answer can go on with: `File csvFile = new File( url.toURI() );` – Abdull Jan 30 '15 at 19:09
9
// assuming a file src/test/resources/some-file.csv exists:

import java.io.InputStream;
// ...
InputStream is = getClass().getClassLoader().getResourceAsStream("some-file.csv");
Abdull
  • 26,371
  • 26
  • 130
  • 172
5
import org.apache.commons.io.FileUtils;
...
 final File dic = FileUtils.getFile("src","test", "resources", "csvFile");

since Apache Commons IO 2.1.

simpatico
  • 10,709
  • 20
  • 81
  • 126
0

This solution need not lib's. First create a util class to access the resource files.

public class TestUtil(Class classObj, String resourceName) throws IOException{
   URL resourceUrl = classObj.getResource(FileSystems.getDefault().getSeparator()+resourceName);
   assertNotNull(resourceUrl);
   return new File(resourceUrl.getFile());
}

Now you just need to call the method with the class of your unitTest and the name of your file in the ressource folder.

File cvsTestFile = TestUtil.GetDocFromResource(getClass(), "MyTestFile.cvs");
swissonid
  • 1,025
  • 1
  • 10
  • 22