简体   繁体   中英

Providing sample data to test JavaFX GUI

I have a JavaFX program that connects to external web API to download some data. I test parts of the logic using mocks in unit tests but now I would like to test the GUI, that is run the program and see how the downloaded data looks like when put in TableView or ListView.

public class Main extends Application {
    private MainController mainController;

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/main.fxml"));
        Parent root = fxmlLoader.load();
        mainController = fxmlLoader.getController();
        primaryStage.setTitle("Title");
        primaryStage.setScene(new Scene(root, 600, 550));
        primaryStage.show();
    }

    @Override
    public void stop() {
        mainController.close();
    }
}

What I tried to do is to have a test method that launches the application using Aplication.launch and then calls a MainController 's method setApi to replace the default API manager object with a mockup. Unfortunately I don't know how to get access to the mainController object. So, my question is - can it be done? If yes - how? If not - what is an other way to test the GUI with sample data?

I use TestFX in combination with JBehave to do the user interface testing. Strictly speaking the latter isn't necessary for your case and you could do just as well with a normal JUnit testcase.

In TestFX you get the stage on which to perform the test, and you pass your scene to it in order to set the user interface.

To follow your example your test would load the FXML, pass the root to TestFX, and then query for the MainController .

As you then have the controller you can manipulate it and use TestFX to simulate clicks, key presses and validate node state.

    @When("the application is closed")
    public void whenTheApplicationIsClosed() throws TimeoutException, InterruptedException {
        clickOn("#filemenu");
        clickOn("#menuitem-quit");
    }

    @Given("a workbench with no changed editors")
    public void givenAWorkbenchWithNoChangedEditors() throws TimeoutException, InterruptedException {
        for (int i = 1; i <= 5; i++) {
            TestEditor editor = new TestEditor(i);
            FxToolkit.setupFixture(() -> workbench.getParts().add(editor));
        }
    }

    @Given("a workbench with a single changed editor")
    public void givenSingleChanged() throws TimeoutException {
        TestEditor testEditor = new TestEditor(1);
        FxToolkit.setupFixture(() -> workbench.getParts().add(testEditor));

        List<Part> parts = workbench.getParts();

        for (int i = 2; i < 5; i++) {
            final Editor editor = new TestEditor(i);
            FxToolkit.setupFixture(() -> parts.add(editor));
        }

        FxToolkit.setupFixture(testEditor::change);
    }

    @Then("a save content dialog is shown")
    public void thenASaveContentDialogIsShown() throws InterruptedException {
        verifyThat(Messages.getString("app.exit.unsavedchanges.header"), NodeMatchers.isVisible());
        clickOn(Messages.getString("app.exit.unsavedchanges.quit"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM