How to test method of JavaFX controller?

2019-07-20 13:11发布

I was trying to use TestFX to test my application. I would like to run the test for the method of my controller.

Main.java:

public class Main extends Application {
    try{
        new Flow(ManageCtrl.class).startInStage(primaryStage);
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, null, ex);
    }
}

ManageCtrl.java:

@ViewController("/FPManage.fxml")
public class ManageCtrl extends AnchorPane {

    @FXML // fx:id="email"
    private TextField email; // Value injected by FXMLLoader

    public void setEmail(String address) {
        this.email.setText(address);
    }
}

ManageCtrlTest.java:

public class ManageCtrlTest extends ApplicationTest {

    @Override
    public void start(Stage stage) {
        try {
            new Flow(ManageCtrl.class).startInStage(stage);
        } catch (FlowException ex) {
            Logger.getLogger(ManageCtrlTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Test
    public void testSetEmail() {
        ManageCtrl instance = new ManageCtrl();
        instance.setEmail("test@gmai.com");

        assertEquals("test@gmail.com", ((TextField)GuiTest.find("#email")).getText());
    }
}

But I get the following Exception:

testSetEmail Failed: java.lang.illegalStateException: Not on FX application thread; currentThread = Test worker
java.lang.illegalStateException: Not on FX application thread; currentThread = Test Worker

Thanks for the help.

1条回答
唯我独甜
2楼-- · 2019-07-20 13:57

The IllegalStateException is related to the nature of JavaFX and TestFX.

ManageCtrl extends from AnchorPane which is one of JavaFX's Scene objects that all need to be constructed within the JavaFX thread (also known as JavaFX application thread or JavaFX user thread). You can use ApplicationTest#interact to construct ManageCtrl within the JavaFX thread:

interact(() -> {
    ManageCtrl controller = new ManageCtrl();
    controller.setEmail("test@gmail.com");
});

However this will throw a NullPointerException which is caused by the nature of DataFX which is used with new Flow(ManageCtrl.class).

new Flow(ManageCtrl.class).startInStage(stage) will inject all @FXML-annotated fields in the controller with objects defined in your @ViewControllernew ManageCtrl() won't. We can solve this problem by constructing ManageCtrl into the field controller before the test:

@Override
public void start(Stage stage) throws Exception {
    Flow flow = new Flow(ManageCtrl.class);

    // create a handler to initialize a view and a sceneRoot.
    FlowHandler handler = flow.createHandler();
    StackPane sceneRoot = handler.start();

    // retrieve the injected controller from the view.
    FlowView view = handler.getCurrentView();
    controller = (ManageCtrl) view.getViewContext().getController();

    // attach the sceneRoot to stage.
    stage.setScene(new Scene(sceneRoot));
    stage.show();
}

You can now test your controller with:

@Test
public void should_set_email() throws Exception {
    // when:
    interact(() -> {
        controller.setEmail("test@gmail.com");
    });

    // then:
    verifyThat("#email", hasText("test@gmail.com"));
}

The whole thing is detailed in an issue on GitHub. I've also created a pull request on Bitbucket that tries to simplify testing on this regard.

查看更多
登录 后发表回答