gpt4 book ai didi

java - 在 Maven test/resources 目录中创建和删除文件

转载 作者:行者123 更新时间:2023-12-02 03:27:39 25 4
gpt4 key购买 nike

我正在尝试为可以写入和删除文件的 java servlet 编写一些单元测试。我有用于我的开发和生产版本的 config.properties 文件,以及仅在测试期间调用的测试/资源中的文件。我写入一个临时文件,该文件后来被删除。

canceled.filepath=src/test/resources/Cancel.txttemp.filepath=src/test/resources/Cancel_temp.txt

我的问题是 servlet 抛出错误,提示我无法删除临时文件。我认为这是由于权限错误造成的。有没有什么地方可以制作这些文件,以便我的单元测试并拥有写入/删除的完全权限?

谢谢

最佳答案

使用 Junit 4 TemporaryFolder 规则为您管理文件系统交互。

public class MyTestClass {
//MUST be public
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void test() throws Exception{
//You can create new files.
File tmpFile = tempFolder.newFile();
System.out.println(tmpFile.getAbsolutePath());
System.out.println(tmpFile.exists());

//Or new Folders
File myFolder = tempFolder.newFolder("My_Folder");
System.out.println(myFolder.getAbsolutePath());
System.out.println(myFolder.exists());

//or a combination of them.
File newFileInMyFolder = tempFolder.newFile("My_Folder\\subfile.txt");
System.out.println(newFileInMyFolder.getAbsolutePath());
System.out.println(newFileInMyFolder.exists());

// The Junit rule uses the system property 'java.io.tempdir' to create them, and it handles the cleanup outside
// the scope of your test!
}
}

输出:

    C:\Users\Jeremiah\AppData\Local\Temp\junit4815976615865849247\junit796088998678325697.tmp
true
C:\Users\Jeremiah\AppData\Local\Temp\junit4815976615865849247\My_Folder
true
C:\Users\Jeremiah\AppData\Local\Temp\junit4815976615865849247\My_Folder\subfile.txt
true

After the text executes, the Rule implementation handles all clean-up, as long as the files were created using the Rule.

根据您的问题,您可以在 @Before block 中设置系统属性,然后相信它们存在于 Activity 测试的上下文中。

public class MyServletTest {
//MUST be public
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Before
public void setTestPaths() throws Exception {
File cancelFile = tempFolder.newFile("Cancel.txt");
File cancelTemp = tempFolder.newFile("Cancel_temp.txt");

System.setProperty("canceled.filepath", cancelFile.getAbsolutePath());
System.setProperty("temp.filepath", cancelTemp.getAbsolutePath());
}

@After
public void restorePaths() {
//FIXME: The JVM will be reused, if you have any other tests relying on the system properites they will be getting the values set in the BEFORE block.
}

@Test
public void checkSysVars() {
String cancelPath = System.getProperty("canceled.filepath");
String tmpPath = System.getProperty("temp.filepath");

File cancelFile = new File(cancelPath);
File cancelTemp = new File(tmpPath);
System.out.println(cancelFile.getAbsolutePath());
System.out.println(cancelFile.exists());
System.out.println(cancelTemp.getAbsolutePath());
System.out.println(cancelTemp.exists());

}
}

再次,控制台输出:

C:\Users\Jeremiah\AppData\Local\Temp\junit7380201043103411996\Cancel.txt
true
C:\Users\Jeremiah\AppData\Local\Temp\junit7380201043103411996\Cancel_temp.txt
true

关于java - 在 Maven test/resources 目录中创建和删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38598783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com