Here is the layout of my project:
src/
test/
resources/
ares/
file1.xml
file2.xml
Here is the layout of the Jenkins workspace:
my-module/
target/
test-classes/
ares/
file1.xml
file2.xml
Under eclipse the tests run without any error. On Jenkins, the tests just fail. Jenkins is unable to locate the resources. Below are some output from the test execution:
MyClass.class.getResourceAsStream(/ares/file1.xml) => java.io.BufferedInputStream@4f4b2f1a
MyClass.class.getResourceAsStream(ares/file1.xml) => null
Thread.currentThread().getContextClassLoader().getResourceAsStream(/ares/file1.xml) => null
Thread.currentThread().getContextClassLoader().getResourceAsStream(ares/file1.xml) => java.io.BufferedInputStream@5d402eeb
MyClass.class.getClassLoader().getResourceAsStream(/ares/file1.xml) => null
MyClass.class.getClassLoader().getResourceAsStream(ares/file1.xml) => java.io.BufferedInputStream@20c87621
MyClass.class.getResourceAsStream(/ares/file1.xml) => null
MyClass.class.getResourceAsStream(ares/file1.xml) => null
Thread.currentThread().getContextClassLoader().getResourceAsStream(/ares/file1.xml) => null
Thread.currentThread().getContextClassLoader().getResourceAsStream(ares/file1.xml) => null
MyClass.class.getClassLoader().getResourceAsStream(/ares/file1.xml) => null
MyClass.class.getClassLoader().getResourceAsStream(ares/file1.xml) => null
As you can see Jenkins doesn't find my resource.
What am I missing?
This error can be caused by including a lot of screenshots in test cases. To resolve this error we recommend increasing the heap size memory in the Jenkins job.
In Jenkins, in the pipeline where failure occurred, in the pane, select the latest build, and click Console Output. On the Console Output page, check the logs to find the reason for the failure.
I had this problem with similar symptoms but different cause and different solution.
In my case the issue was that the Jenkins server was a Windows machine and the full path on the server to the location of the resources started with C:\Program Files (x86)\...
with spaces. Those spaces get encoded to %20
if you need to get is as a File
instead of a stream using new File(getClass().getResource(fileName).getFile())
. Here fileName
is a string that contains the name of the resource. I solved the problem by adding a call to URLDecoder.decode
. This creates no problems when there are no spaces or you're not on Windows (as far as I've seen) but it solves the problem if you get a space in the name somewhere along the line. The full call is then:
new File(URLDecoder.decode(getClass().getResource(fileName).getFile(), "UTF-8"))
I pieced this together from several questions, but none put it all together for the Jenkins case, hence my answer here. Other relevant Q&A:
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