I have some tests that I wrote in JUnit built with Maven that uses the File and FileWriter API, and the tests work on my local machine but not when I try to build it in Jenkins. Is there a special convention that I have to follow to get my tests to work in Jenkins?
In my test file I am initializing some paths for where my resource folder is located:
private String resourcesFolderPath = new File("src/test/resources").getAbsolutePath();
private String outputFolderPath = resourcesFolderPath + "/output";
private File outputFolder = new File(outputFolderPath);
And here is a test that is failing in Jenkins:
@Test
public void test() throws Exception {
File file = new File(outputFolder.list()[0]); // File not found exception at this line
if (!file.exists()) file.mkdirs(); // I added this line after Jenkins build failed, did not fix the problem
String fileListName = file.getName();
FileReader fileReader = new FileReader(outputFolderPath + "/" + fileListName);
BufferedReader buffReader = new BufferedReader(fileReader);
String firstFileName = buffReader.readLine();
assertTrue(firstFileName.contains("test1.txt"));
buffReader.close();
}
The output folder contains the file that I want to test, but Jenkins fails with a FileNotFoundException:
java.io.FileNotFoundException: /home/jenkins/workspace/utils-feature-branches-ci/file-list/src/test/resources/output/File_List.txt (No such file or directory)
The strange thing is that this test works in my local machine when building with Maven, but just not on Jenkins. So my question is how do I modify my code so that this test works with Jenkins? Any help would be appreciated!