如何使用JavaFX Filechooser对方法进行单元测试

时间:2014-05-21 20:16:52

标签: java unit-testing junit javafx

如何为启动JavaFX Filechooser的方法编写JUNIT测试? 单元测试卡住等待来自该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;
}

1 个答案:

答案 0 :(得分:0)

你可以为myController添加一个模拟器,它可以实现相同的接口,但是mock只会在没有任何用户干预的情况下返回文件路径。