JavaFX 8 的基本 JUnit 测试 [英] Basic JUnit test for JavaFX 8

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

问题描述

我想为 JavaFX 8 应用程序创建基本的 JUnit 测试.我有这个简单的代码示例:

I want to create basic JUnit test for JavaFX 8 application. I have this simple code sample:

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tabs");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);
        TabPane tabPane = new TabPane();
        BorderPane borderPane = new BorderPane();
        for (int i = 0; i < 5; i++) {
            Tab tab = new Tab();
            tab.setText("Tab" + i);
            HBox hbox = new HBox();
            hbox.getChildren().add(new Label("Tab" + i));
            hbox.setAlignment(Pos.CENTER);
            tab.setContent(hbox);
            tabPane.getTabs().add(tab);
        }
        // bind to take available space
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());

        borderPane.setCenter(tabPane);
        root.getChildren().add(borderPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我目前只有这个代码:

import javafx.application.Application;
import javafx.stage.Stage;
import org.junit.BeforeClass;

public class BasicStart extends Application {

    @BeforeClass
    public static void initJFX() {
        Thread t = new Thread("JavaFX Init Thread") {
            @Override
            public void run() {
                Application.launch(BasicStart.class, new String[0]);
            }
        };
        t.setDaemon(true);
        t.start();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // noop
    }
}

你能告诉我如何为上述代码创建 JUnit 测试吗?

Can you tell me how I can create JUnit test for the above code?

推荐答案

我使用 Junit 规则在 JavaFX 线程上运行单元测试.详情见这篇文章.只需从该帖子中复制该类,然后将此字段添加到您的单元测试中即可.

I use a Junit Rule to run unit tests on the JavaFX thread. The details are in this post. Just copy the class from that post and then add this field to your unit tests.

@Rule public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();

此代码适用于 JavaFX 2 和 JavaFX 8.

This code works for both JavaFX 2 and JavaFX 8.

这篇关于JavaFX 8 的基本 JUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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