How to get a test resource file?
Asked Answered
D

6

82

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

Decant answered 3/4, 2011 at 12:19 Comment(1)
Found simple solution (Java7+) here : https://mcmap.net/q/98178/-how-to-get-the-path-of-src-test-resources-directory-in-junitLeroi
R
91

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
Renaterenato answered 3/4, 2011 at 17:27 Comment(5)
what I was really looking for was abstraction from providing the path to the file, as in getResourceAsStream. Otherwise new File/getFile is more straightforwardDecant
url is null for me with that statementDoura
@Doura 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)Renaterenato
the resolution of the file ends up including the package of the current class. @DouraCellulitis
Need to use new File(url.toURI()); to correctly handle spaces in file path.Unlawful
T
24

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

InputStream stream = Thread.currentThread().getContextClassLoader()
    .getResourceAsStream("YOURFILE.CSV");
Theologize answered 3/4, 2011 at 12:46 Comment(3)
but I want a java.io.File and not an InputStream.Decant
then use getClass().getResource("/src/test/resources/YourFile.csv"); download.oracle.com/javase/6/docs/api/java/lang/Class.htmlAwake
so, FileUtils.toFile(Thread.currentThread().getClass().getResource("/src/test/resources/YourFile.csv"));Decant
S
14

with guava

import com.google.common.io.Resources;
URL url = Resources.getResource("YourFile.csv");
Selfsupport answered 11/12, 2013 at 8:33 Comment(1)
as @Decant wants a java.io.File, this answer can go on with: File csvFile = new File( url.toURI() );Kc
K
9
// assuming a file src/test/resources/some-file.csv exists:

import java.io.InputStream;
// ...
InputStream is = getClass().getClassLoader().getResourceAsStream("some-file.csv");
Kc answered 29/11, 2016 at 20:40 Comment(0)
D
4
import org.apache.commons.io.FileUtils;
...
 final File dic = FileUtils.getFile("src","test", "resources", "csvFile");

since Apache Commons IO 2.1.

Decant answered 3/4, 2011 at 12:19 Comment(0)
H
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");
Homespun answered 5/6, 2014 at 16:11 Comment(2)
where can I get FileSystems class ?Liquor
@ToKra java.nio.file.FileSystemsBurglary

© 2022 - 2024 — McMap. All rights reserved.