2016-07-26 106 views
0

我正在嘗試爲可以寫入和刪除文件的java servlet編寫一些單元測試。我有我的dev和prod版本的config.properties文件,以及一個只在測試期間被調用的測試/資源中的文件。我寫入一個稍後刪除的臨時文件。在Maven測試/資源目錄中創建和刪除文件

canceled.filepath =的src /測試/資源/ Cancel.txt temp.filepath =的src /測試/資源/ Cancel_temp.txt

我的問題是,我得到說我可以從servlet拋出一個錯誤不要刪除臨時文件。我認爲這是由於權限錯誤。有沒有我可以製作這些文件的地方,以便我的單元測試並具有完整的許可/刪除?

感謝

+2

爲什麼不建立在你的系統默認的臨時目錄下的臨時文件? ['Files.createTempFile'](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#createTempFile-java.nio.file.Path-java.lang。字符串java.lang.String中-java.nio.file.attribute.FileAttribute ...-)。使用JUnit,你也可以使用['TemporaryFolder'](http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html) – Tunaki

+0

配置文件爲servlet提供了一個路徑,想要執行文件操作。所以servlet本身正在寫和刪除這些文件。我確實試圖給它一個臨時目錄的路徑,但刪除仍然沒有完成 –

+0

該servlet只能通過ServletConfig來獲得配置。如果你這樣做,那麼你可以在你的測試中調用'Servlet.init(ServletConfig)'方法並指定一個測試配置。 –

回答

2

使用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 
+0

真棒,謝謝 –

相關問題