Maven 如何让文件操作在Jenkins中工作?
我在JUnit中编写了一些测试,这些测试是用Maven构建的,它使用文件和FileWriter API,这些测试在我的本地机器上运行,但在Jenkins中构建时却无法运行。有没有一个特殊的惯例,我必须遵循,让我的测试工作在詹金斯 在测试文件中,我正在初始化资源文件夹所在的一些路径:Maven 如何让文件操作在Jenkins中工作?,maven,jenkins,junit,Maven,Jenkins,Junit,我在JUnit中编写了一些测试,这些测试是用Maven构建的,它使用文件和FileWriter API,这些测试在我的本地机器上运行,但在Jenkins中构建时却无法运行。有没有一个特殊的惯例,我必须遵循,让我的测试工作在詹金斯 在测试文件中,我正在初始化资源文件夹所在的一些路径: private String resourcesFolderPath = new File("src/test/resources").getAbsolutePath(); private String ou
private String resourcesFolderPath = new File("src/test/resources").getAbsolutePath();
private String outputFolderPath = resourcesFolderPath + "/output";
private File outputFolder = new File(outputFolderPath);
詹金斯的一个测试失败了:
@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();
}
输出文件夹包含我要测试的文件,但Jenkins失败,出现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)
奇怪的是,当使用Maven构建时,这个测试可以在我的本地机器上运行,但不能在Jenkins上运行。所以我的问题是如何修改我的代码,使这个测试与Jenkins一起工作?任何帮助都将不胜感激 通过JUnit运行测试类时,
src/Test/resources
将是您的类路径
因此,添加这一行就足够了
File file = new File("output");
谢谢,但我的问题并不是关于JUnit,我已经使用的输出路径,但它在Jenkins上不起作用。如果你想从resources文件夹中读取一些内容,你必须通过
getClass().getResourcesStream(“/whater.properties”)
。像你那样阅读目录列表有一个假设。文件的顺序…文件的顺序取决于OS/JDK,因此您不必假设获得文件名的顺序。此外,不要使用文件名来读取src/test/resources
目录。