4

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.

chiahao
  • 296
  • 4
  • 16
  • I've never used TestFX, so there might be a specific solution for that framework. Andy Till created a [JUnit rule for testing on the JavaFX application thread](https://gist.github.com/andytill/3835914). Perhaps you could adapt that for your purpose, though I would expect a framework named TestFX to handle such things natively. – jewelsea Mar 11 '15 at 20:49
  • I'm sorry to ask in 2 different place. But I got the answer from the author: [#207](https://github.com/TestFX/TestFX/issues/207). The point is to use DataFX API: `Flow flow = new Flow(ManageCtrl.class); FlowHandler handler = flow.createHandler(); stage.setScene(new Scene(handler.start())); stage.show(); FlowView view = handler.getCurrentView(); controller = (ManageCtrl) view.getViewContext().getController();` With That controller, we can call method inside. – chiahao Mar 12 '15 at 01:41
  • 5 min is too short for me to preview the result. >.< `Flow flow = new Flow(ManageCtrl.class);` Still failed to add a line break, but the help says add 2 space will add
    . Also failed to add high light syntax.
    – chiahao Mar 12 '15 at 03:30
  • you can [answer your own question](http://stackoverflow.com/help/self-answer) and mark it as correct. I suggest you do that by summarizing the offsite information (so the answer is self-contained on stack overflow). That is better than posting code into the comments. – jewelsea Mar 12 '15 at 04:14

1 Answers1

2

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.