首页 > 解决方案 > 表格视图复选框的 Javafx 单元测试

问题描述

我是 JavaFx 的新手,或者更准确地说,我不知道如何通过 Google 搜索我想要的东西。目前,我正在一个使用 JavaFx 作为 GUI 的 Gradle 项目中工作。我已经定义了一个表格视图和几个表格列。在一个表格列中,我用复选框填充它。同样,我不知道这些条款,因为我只是从互联网上复制它们。

这就是我定义表格列的方式。

@FXML
private TableColumn<Section, Boolean> enrollStatus;

有一个 observableList。据我所知,它可以以某种方式神奇地与 tableView 链接。

private ObservableList<Section> observableSections = FXCollections.observableArrayList();

我在我的控制器类中初始化如下:

@FXML
    public void initialize() {
        courseCode.setCellValueFactory(new PropertyValueFactory<Section, String>("courseCode"));
        sectionCode.setCellValueFactory(new PropertyValueFactory<Section, String>("sectionCode"));
        courseName.setCellValueFactory(new PropertyValueFactory<Section, String>("courseName"));
        instructorName.setCellValueFactory(new PropertyValueFactory<Section, String>("instructorName"));
        enrollStatus.setCellValueFactory(new PropertyValueFactory<Section, Boolean>("enrollStatus"));
        enrollStatus.setCellFactory(CheckBoxTableCell.forTableColumn(enrollStatus));
        tableView.setEditable(true);
        enrollStatus.setEditable(true);
        tableView.setItems(observableSections);
    }

控制器中还有一个功能,当我用 Section 填充 tableView 后,它会为每个复选框添加侦听器。

void populateSectionsList(boolean addListener, List<Course> courses) {
    observableSections.clear();
    for (Course course : courses) {
        List<Section> sections = course.getSections();
        if (addListener) {
            for (Section section : sections) {
                if (section.getEnrollStatus() == false) { //if true, listener added already
                    section.enrollStatusProperty().addListener(
                        (observable, oldvalue, newvalue) -> {
                            if (oldvalue == false && newvalue == true) {
                                enrolledSections.add(section);

                            if (oldvalue == true && newvalue == false)
                                for (int i = 0; i < enrolledSections.size(); ++i)
                                    if (enrolledSections.get(i).getEnrollStatus() == false) {
                                        enrolledSections.remove(i);
                                        updateTimeTable();
                                    }
                            }
                    );
                    }
            }
        }
        observableSections.addAll(sections);
    }
}

至此,tableView 中的所有复选框都是在运行时动态分配的,也就是说它们不在 fxml 文件中。因此,我无法在单元测试中执行以下任务:

 @Test
     public void testbuttonSfqEnrollCourse() {
         clickOn("#tabMain");
         clickOn("#buttonSearch");
         clickOn("#tabSfq");
         clickOn("#buttonSfqEnrollCourse");
     }

所以我的问题是,如何在测试文件中编写代码,以便在 Gradle Task 中按下“test”后,它会弹出一个窗口并自动单击 tableview 中的复选框?

标签: javafxjunit

解决方案


推荐阅读