Unable to access test resources from junit tests

junit read file from src/test/resources
junit src/test/resources
maven test/resources not in classpath
spring boot test/resources
maven test resources directory
read file from test/resources java
get file path from resource folder java
java 8 read file from resources

Edit: Turns out I hadn't rebuilt the program in a while. If all the other answers for this kind of problem are not working for you, try rebuilding! Thanks Timo!

I've looked at a ton of answers and tried a bunch of stuff. I know I must be doing something extremely minor wrong. If anyone has a quick tip please let me know, this is slowly killing me.

My directory setup is:

I'm trying to read in testDom.xml with

File testFile = new File(getClass().getResource("testDom.xml").getFile());

I've tried loading from the class:

File testFile = new File(GemsDomGetChildrenTest.class.getResource("testDom.xml").getFile());

I've tried both with the path as

/lambda-compiler/src/test/resources/testDom.xml
/src/test/resources/testDom.xml
/test/resources/testDom.xml
/resources/testDom.xml
/testDom.xml
testDom.xml

And I've tried all those paths without leading '/' as well.

The resources folder also seems to be added as a test resource in IntelliJ too:

I get that this is probably a duplicative question and has been asked a million times, but I'm really struggling to see what I'm doing differently than all the other questions I've viewed so far. Any help would be greatly appreciated.


It should be getResource("/testDom.xml")

Get the Path of the /src/test/resources Directory in JUnit, Unable to access test resources from junit tests. maven test resources not copied to target java test/resources maven test/resources directory junit test file  Any file under src/test/resources is often copied to target/test-classes. To access these resource files in JUnit we can use the class’s resource. It will locate the file in the test’s classpath /target/test-classes.


Should work:

URL resource = GemsDomGetChildrenTest.class.getClassLoader().getResource("testDom.xml");
File file = new File(resource.toURI());

Unable to access test resources from junit tests, Try putting a file into the src/main/resources folder, and read the file with following printFile(file); } // get file from classpath, resources folder private File Unit Test​. src/test/resources/xml/data.xml. <test> <case id='1'> testClassLoader\target\​classes\file\test.txt (The system cannot find the path specified). Sometimes during unit testing, we might need to read some file from the classpath or pass a file to an object under test. Or, we may have a file in src/test/resources with data for stubs that could be used by libraries like WireMock. In this tutorial, we'll show how to read the path of the /src/test/resources directory.


Alright so Timo was able to figure it out. I hadn't rebuilt the project for a while (:facepalm:). Coming from the python world here, where things are a little different. Thanks for working through this with me everyone, it really means a lot!

Java - Read a file from resources folder, While running Junit testing it seems that I cannot open a specific schema 2018-​11-20 15:13:44 ERROR SchemaRegistry:115 a- Unable to get jschema/src/test​/resources/schema/com-snaplogic-snap-template_2.schema Proper way to access file resources in junit tests As I was refactoring some JUnit tests recently I was reminded of an important fact on the proper way to read in a file. In maven any file under /src/test/resources is automatically copied over to /target/test-classes .


JUnit Unable to open schema file - Developing Snaps, Yes, I want to use the result to access a resource about test. But now, the root path is not what is expected. Caused unit tests to fail to access  @yegor256 since it's a unit test closing resources is particularly important. "Unit" tests should be fast and self contained, leaving resources open, potentially for the duration of the test run, means at best your tests run slower, and at worst fail in difficult-to-diagnose ways. – dimo414 May 27 '14 at 7:24


Problem to get the root path of class when run a test · Issue #190 , where my sub-project tests are not able to access `src/test/resources` for Attempt to access the file in a Spock Unit Test (probably any Unit  I managed to get it working better by creating a test-resources directory parallel to the test directory (!).. Still seeing intermittent problems with this, though. Those times, it's possible to open the IntelliJ project settings and define the test resources as source instead (the Gradle project will remain unchanged).


'src/test/resources' fails in Gradle Multi-module project – IDEs , These tests have access to the real device and its resources and are useful to unit test functionality which cannot be easily mocked by mocking  Organizing Resource Files for Tests Using Maven and Java We take a look at how to organize your resource files for testing in your Java application by making use of Maven. Here's how to ensure