JUNIT not identifying resources under src/test folder
you can use classLoader like this :
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("somefile").getFile());
System.out.println(file.getAbsolutePath());
Regards.
Why Can't I access src/test/resources in Junit test run with Maven?
Access MainConfig.xml
directly. The src/test/resources
directory contents are placed in the root of your CLASSPATH.
More precisely: contents of src/test/resources
are copied into target/test-classes
, so if you have the following project structure:
.
└── src
└── test
├── java
│ └── foo
│ └── C.java
└── resources
├── a.xml
└── foo
└── b.xml
It will result with the following test CLASSPATH contents:
/foo/C.class
/a.xml
/foo/b.xml
To actually access the files from Java source, use getClass().getResource("/MainConfig.xml").getFile()
.
Java path to file in src/main/resources
The problem was that I have not included the resource in the maven build in pom file. When I included the following it worked with abcManager.class.getResource("/abc.access")
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>abc.access</include>
</includes>
</resource>
</resources>
</build>
Unable to access test resources from junit tests
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!
file not found on path src/test/resources/
You should never depend on File
. Load InputStream
from classpath.
Both src/main/resources and src/test/resources are in the classpath if you use the Maven standard directory structure.
I tend to like the Apache Commons IO library and Spring ClassPathResource
.
Related Topics
Calculating the Angle Between Two Lines Without Having to Calculate the Slope (Java)
How to Make a Java Program Quit When "Q" Is Inputted, Issue Is the Default Input Variable Is Double
Regular Expression to Match a Backslash Followed by a Quote
How to Get the Parent Element Name of a Child Element in Json
Changing Scene When a Button Is Pressed Fxml
Custom Pagination by Array List
How to Mark Cucumber Junit Step as Failed But Continue
How to Change Background Colour to Specific Viewholder Items in a Recycleview
Beanutils Copyproperties API to Ignore Null and Specific Propertie
Converting Date to 08:00:00.000+0000
Spring Boot API Call With Multiple @Requestparam
Multiple Queries Executed in Java in Single Statement
Enable Jackson Deserialization of Empty Objects to Null
How to Find the Number of Times If or Else Code Has Executed
Round Off a Double While Maintaining the Trailing Zero