I am writing junit
tests, where I need to read a file from src/test/resources
I have a following code in my src/test/java/some_packages/UserTests
:
InputStream inputStream = new FileInputStream(new File("abc.txt"));
The file is of course present, but when I run my test I get java.io.FileNotFoundException: abc.txt (No such file or directory)
I marked resources folder as test resources in intellij
new File("abc.txt")
is relative to the working directory for your tests, not the src/test/resources
directory. To access resources in the src/test/resources
directory, get the resource from the class loader:
InputStream inputStream = getClass().getResourceAsStream("/abc.txt");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With