Reputation: 634
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?
Upvotes: 1
Views: 3927
Reputation: 634
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
Upvotes: 1
Reputation: 1114
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)
Upvotes: 0