Java 在Maven test/resources目录中创建和删除文件
我正在尝试为一个可以写入和删除文件的Javaservlet编写一些单元测试。我有用于开发和产品构建的config.properties文件,以及一个只在测试期间被调用的测试/资源中的文件。我写入一个临时文件,该文件随后被删除 已取消。filepath=src/test/resources/Cancel.txt temp.filepath=src/test/resources/Cancel\u temp.txt 我的问题是servlet抛出了一个错误,说我不能删除临时文件。我认为这是由于权限错误造成的。是否有任何地方我可以使这些文件,使我的单元测试,并有充分的权限,以wirte/删除Java 在Maven test/resources目录中创建和删除文件,java,maven,junit,Java,Maven,Junit,我正在尝试为一个可以写入和删除文件的Javaservlet编写一些单元测试。我有用于开发和产品构建的config.properties文件,以及一个只在测试期间被调用的测试/资源中的文件。我写入一个临时文件,该文件随后被删除 已取消。filepath=src/test/resources/Cancel.txt temp.filepath=src/test/resources/Cancel\u temp.txt 我的问题是servlet抛出了一个错误,说我不能删除临时文件。我认为这是由于权限错误造
谢谢使用Junit 4临时文件夹规则为您管理文件系统交互
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
文本执行后,规则实现将处理所有清理,
只要文件是使用规则创建的
根据您的问题,您可能可以在@Before块中设置系统属性,然后相信它们在活动测试的上下文中存在
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
使用JUnit4临时文件夹规则为您管理文件系统交互
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
文本执行后,规则实现将处理所有清理,
只要文件是使用规则创建的
根据您的问题,您可能可以在@Before块中设置系统属性,然后相信它们在活动测试的上下文中存在
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
为什么不在系统的默认临时目录中创建临时文件。使用JUnit,您还可以使用配置文件acutally为servlet提供执行文件操作的路径。因此servlet本身正在写入和删除文件。我确实尝试给它一个临时目录的路径,但是删除仍然没有完成servlet应该只通过
ServletConfig
获取配置。如果执行此操作,则可以在测试中调用Servlet.init(ServletConfig)
方法并指定测试配置。为什么不在系统的默认临时目录中创建临时文件呢。使用JUnit,您还可以使用配置文件acutally为servlet提供执行文件操作的路径。因此servlet本身正在写入和删除文件。我确实尝试给它一个临时目录的路径,但是删除仍然没有完成servlet应该只通过ServletConfig
获取配置。如果执行此操作,则可以在测试中调用Servlet.init(ServletConfig)
方法并指定测试配置。