web-dev-qa-db-ja.com

JavaFX 8の基本的なJUnitテスト

JavaFX 8アプリケーション用の基本的なJUnitテストを作成したい。私はこの簡単なコードサンプルを持っています:

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テストを作成する方法を教えてください。

18
Peter Penzov

JUnitルールを使用して、JavaFXスレッドでユニットテストを実行します。詳細は この投稿 にあります。その投稿からクラスをコピーして、このフィールドをユニットテストに追加するだけです。

@Rule public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();

このコードは、JavaFX 2とJavaFX 8の両方で機能します。

30
Brian Blonski

最も簡単な方法は次のとおりです。

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.stage.Stage;

import org.junit.Test;

public class BasicStart {

    @Test
    public void testA() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                new JFXPanel(); // Initializes the JavaFx Platform
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        new Main().start(new Stage()); // Create and
                                                        // initialize
                                                        // your app.

                    }
                });
            }
        });
        thread.start();// Initialize the thread
        Thread.sleep(10000); // Time to use the app, with out this, the thread
                                // will be killed before you can tell.
    }

}

それが役に立てば幸い!

7
Magcus

Brian Blonski に基づいています answerJUnit-Testrunner を作成しました。これは基本的に同じことを実行しますが、使用するのが少し簡単です私の意見。これを使用すると、テストは次のようになります。

@RunWith( JfxTestRunner.class )
public class MyUnitTest
{
  @Test
  public void testMyMethod()
  {
   //...
  }
}
4