2014-05-21 83 views
1

如何爲調出JavaFX Filechooser的方法編寫JUNIT測試? 單元測試卡從文件選擇如何使用JavaFX Filechooser測試方法

@Test 
public void testGetPath() { 
    try { 
     myController mc= new myController(); 
     String s = uiController.getPath(); 
     assertNotNull(s); 
    } 



public String getPath() { 
String s = ""; 
Task<Void> t = new Task<Void>() { 

    @Override 
    protected Void call() throws Exception { 
     FileChooser myFileChooser = new FileChooser ("/home/default"); 

     File file = myFileChooser.showSaveDialog(appStage); 

     if (file != null) { 
     s = file.getAbsolutePath().toString(); 
     } 
    } 

}; 

Platform.runLater(t); 

while (!t.isDone()) 
    ; 

return s; 
} 

回答

0

你可以添加爲一個myController的模擬這將實現相同的接口,但模仿只會返回一個文件路徑,無須用戶干預,等待用戶輸入。