如何测试JavaFX控制器的方法? [英] How to test method of JavaFX controller?

查看:98
本文介绍了如何测试JavaFX控制器的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用TestFX来测试我的应用程序.我想对控制器的方法进行测试.

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:

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:

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:

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());
    }
}

但是我得到以下异常:

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

感谢您的帮助.

推荐答案

IllegalStateException与JavaFX和TestFX的性质有关.

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

ManageCtrlAnchorPane的扩展,而AnchorPane是JavaFX的Scene对象之一,都需要在JavaFX线程(也称为JavaFX应用程序线程或JavaFX用户线程)中构造.您可以使用ApplicationTest#interact在JavaFX线程中构造ManageCtrl:

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");
});

但是,这将抛出NullPointerException,这是由 DataFX的性质引起的,该数据与new Flow(ManageCtrl.class)一起使用.

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)不会使用您在@ViewControllernew ManageCtrl()中定义的对象插入控制器中所有带@FXML注释的字段.我们可以通过在测试前将ManageCtrl构造到字段controller中来解决此问题:

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"));
}


有关整个内容的详细信息,请参见GitHub上的问题.我还创建了一个对Bitbucket的拉取请求,它试图简化对这方面.


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.

这篇关于如何测试JavaFX控制器的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆