How to Read a Text-File Resource into Java Unit Test

How to read a text-file resource into Java unit test?

Finally I found a neat solution, thanks to Apache Commons:

package com.example;
import org.apache.commons.io.IOUtils;
public class FooTest {
@Test
public void shouldWork() throws Exception {
String xml = IOUtils.toString(
this.getClass().getResourceAsStream("abc.xml"),
"UTF-8"
);
}
}

Works perfectly. File src/test/resources/com/example/abc.xml is loaded (I'm using Maven).

If you replace "abc.xml" with, say, "/foo/test.xml", this resource will be loaded: src/test/resources/foo/test.xml

You can also use Cactoos:

package com.example;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.TextOf;
public class FooTest {
@Test
public void shouldWork() throws Exception {
String xml = new TextOf(
new ResourceOf("/com/example/abc.xml") // absolute path always!
).asString();
}
}

Reading a resource file in JUnit test

The classloader uses the classpath to load resources. Your classes are, most probably, in the package com.au.myapp.util. Not in the package test.com.au.myapp.util.

The directory structure matched the package structure. That means that the directories src and test are both source roots.

Since your file is in the directory com/au/myapp/resources under a source root, its package is com.au.myapp.resources.

So you need

classLoader.getResource("com/au/fitbits/resources/input.txt");

This is a resource, loaded from the classpath. It might be a file now, because you're in development mode, and classes are loaded directly from the file system. But once in production, they won't be loaded from the file system anymore, but from a jar file. So you can't use file IO to read the content of this resource. So use

classLoader.getResourceAsStream("com/au/fitbits/resources/input.txt")

and read from this stream.

How to get a test resource file?

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

How do I access a text file for JUnit test in Android?

Prefix the file path with /.

Basically, you'd do something like this:

File helloBleprintJson = new File(
getClass().getResource("/helloBlueprint.json").getPath());

Above snippet is taken from here.

How to get the path of src/test/resources directory in JUnit?

Try working with the ClassLoader class:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("somefile").getFile());
System.out.println(file.getAbsolutePath());

A ClassLoader is responsible for loading in classes. Every class has a reference to a ClassLoader. This code returns a File from the resource directory. Calling getAbsolutePath() on it returns its absolute Path.

Javadoc for ClassLoader: http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html

How to read resource file from unit test?

Apparently it was a bug, should be fixed now:
https://issuetracker.google.com/issues/63612779

Thank you all for your help

How to use existing text files for JUnit tests

You can put the file in the root of the classpath (/bin, e.g.) and then you can access it as stream using

getClass().getClassLoader().getResourceAsStream("/test.txt")

This would even work, if your classes are packaged as a jar file.

If you need access to the file, you could use

File file = new File(getClass().getClassLoader().getResource("/test.txt").toURI());

Naturally, if you want to put your text file next to your test class or in some other folder, you can use

File file = new File(getClass().getClassLoader().getResource("/net/winklerweb/somepackage/test.txt").toURI());

or any other path ...

How to read file from src/main/resources relative in a JUnit test?

I ended placing the testfiles directly into /src/main/resources without subpackage, and using.

PropertiesLoaderUtils.loadProperties(new ClassPathResource(filename));



Related Topics



Leave a reply



Submit