【问题标题】:Unit test for save method failing with error pointing at fc.showSaveDialog(null)保存方法的单元测试失败,错误指向 fc.showSaveDialog(null)
【发布时间】:2019-08-20 23:57:58
【问题描述】:

根据我的标题

fc.showSaveDialog(null)

我对 save 方法的单元测试失败并指向该语句所在的行。

我意识到它可能会失败,因为我将它设置为 null 而不是舞台,但我也想了解为什么在这种情况下它不能为 null。

public static void save(String text) throws IOException {
        FileChooser fc = new FileChooser();

        //Start file chooser in project directory
        String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();
        fc.setInitialDirectory(new File(currentPath));

        // Define extension filters
        FileChooser.ExtensionFilter txtFilter = new FileChooser.ExtensionFilter("Text Files (*.txt)", "*.txt");
        FileChooser.ExtensionFilter pdfFilter = new FileChooser.ExtensionFilter("PDF Files (*.pdf)", "*.pdf");
        fc.getExtensionFilters().addAll(txtFilter, pdfFilter);


//////////////////////////////THIS LINE
        File file = fc.showSaveDialog(null);
///////////////////////////////////////



        if (file != null) {
            FileChooser.ExtensionFilter selectedFilter = fc.getSelectedExtensionFilter();

            if (selectedFilter == txtFilter) {
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write(text);
                writer.close();
            } else if (selectedFilter == pdfFilter) {
                savePdf(text, file);
            }
        }   
    }

这是我的单元测试。按键模拟的目的是在文件选择器弹出时输入 A,但我的测试在失败之前并没有做到那么远。

    @Test
    void testSave() throws IOException {

        String text = "hello world";
        FileFunctions.save(text);

        Robot robot;
        try {
            robot = new Robot();
            // Simulate a key press
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

如有必要,我可以添加整个故障跟踪,但主要部分是

java.lang.IllegalStateException: 这个操作是允许的 仅限事件线程; currentThread = 主要在 com.sun.glass.ui.Application.checkEventThread(Application.java:443) 在 com.sun.glass.ui.CommonDialogs$ExtensionFilter.(CommonDialogs.java:75) 在 com.sun.javafx.tk.quantum.QuantumToolkit.convertExtensionFilters(QuantumToolkit.java:1614) 在 com.sun.javafx.tk.quantum.QuantumToolkit.showFileChooser(QuantumToolkit.java:1557) 在 javafx.stage.FileChooser.showDialog(FileChooser.java:416) 在 javafx.stage.FileChooser.showSaveDialog(FileChooser.java:392) 在 assignment1.FileFunctions.save(FileFunctions.java:87) 在 assignment1.unitTests.testSave(unitTests.java:18) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)在 java.lang.reflect.Method.invoke(未知来源)

【问题讨论】:

    标签: java unit-testing filechooser


    【解决方案1】:

    在 FileChooser.java 的源代码中,似乎处理了该方法的空参数:

    private List<File> showDialog(final Window ownerWindow,
                                      final FileChooserType fileChooserType) {
            FileChooserResult result = Toolkit.getToolkit().showFileChooser(
                    (ownerWindow != null) ? ownerWindow.getPeer() : null,
        .......................................................................
    
    

    所以我认为问题出在其他地方。

    来源:https://github.com/javafxports/openjdk-jfx/blob/4f0addff16de7182752bc60541f44baeb0e99d2a/modules/javafx.graphics/src/main/java/javafx/stage/FileChooser.java#L411

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2020-02-14
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多