はあなたのためのファイルシステムの相互作用を管理するための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
、ルールの実装は限りファイルはルールを使用して作成されたように、すべてのクリーンアップ、 を処理します。あなたの質問に基づいて
、あなたはおそらく@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
はなぜ、あなたのシステムのデフォルトの一時ディレクトリに一時ファイルを作成していませんか? ['Files.createTempFile'](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#createTempFile-java.nio.file.Path-java.lang)。 String - java.lang.String - java.nio.file.attribute.FileAttribute ...-)。 JUnitを使うと、['TemporaryFolder'](http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html) – Tunaki
を使うこともできます。設定ファイルは、サーブレットにファイル操作を実行したいしたがって、サーブレット自体がファイルの書き込みと削除を行っています。一時ディレクトリーへのパスを与えようとしましたが、削除はまだ完了しません。 –
サーブレットは 'ServletConfig'によってのみ設定を取得します。これを行うと、テストで 'Servlet.init(ServletConfig)'メソッドを呼び出してテスト設定を指定することができます。 –