JavaFX 8的基本JUnit测试

Pet*_*zov 18 junit javafx junit4 javafx-2 javafx-8

我想为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();
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我只有这个代码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Bri*_*ski 30

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

@Rule public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();
Run Code Online (Sandbox Code Playgroud)

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


Mag*_*cus 7

最简单的方法如下:

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.
    }

}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

  • 如果可以,我会避免使用Thread.sleep().对于很少的测试来说这并不是什么大问题,但是当你进行了数百次测试时,它确实会变慢.看看你是否可以使用某种锁扣. (6认同)

Oli*_*low 5

根据Brian Blonski回答,我创建了一个JUnit-Testrunner,它基本上做同样的事情,但在我看来使用起来更简单一些。使用它,您的测试将如下所示:

@RunWith( JfxTestRunner.class )
public class MyUnitTest
{
  @Test
  public void testMyMethod()
  {
   //...
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 您的链接似乎已损坏。很想看到测试运行者! (2认同)