3

I have a JUnit test file for my code that reads an xml file and converts it to string:

String xml = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\testfile.xml"))); 

The test runs and passes locally, but when I run a Jenkins build, it fails with java.nio.file.NoSuchFileException: src\test\resources\testfile.xml

Do I need to change my file path when pushing?

2
  • 2
    On what OS is your Jenkins server? Commented Oct 12, 2018 at 14:55
  • I believe its Linux. Also update: when I move the all the way out (same path as pom) and removed path spec from filename, Jenkins was able to build. Commented Oct 12, 2018 at 15:18

2 Answers 2

1

Alright, so I figured out my problem and I feel pretty silly. When Java builds on my windows machine, using \ to seperate files is fine; however when Jenkins builds, files need to be seperated with /

i.e. my file names should be

src/test/resources/testfile.xml 
Sign up to request clarification or add additional context in comments.

3 Comments

That's was the point of my question about the operating system.
And do not forget that Linux has a case-sensitive file system while Windows has a case-insensitive one.
stackoverflow.com/questions/64277645/… can help me to overcome this issue
0

you should use resource as stream for this:

InputStream is = getClass().getClassLoader().getResourceAsStream(fileName); if (is != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String xml = reader.lines().collect(Collectors.joining(System.lineSeparator())); } 

(code can be improved a lot ... but it will give you a direction)

(check out try by resource to handle streams)

3 Comments

This doesn't really address the issue: the problem is that Jenkins can't find the file based on the path.
It has to do with the file location .. the test resources folder will be packaged/moved so only the class loader can easily provide the access ... I work with that in my own projects aswell ... In IDEs it mostly is not a problem because the file is not packaged and at the location you expect. This cause the difference. docs.oracle.com/javase/10/docs/api/java/lang/…
Check my above answer; that was the issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.