【发布时间】:2013-08-28 01:47:40
【问题描述】:
我想为 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 测试吗?
【问题讨论】:
-
我建议使用 JemmyFX - 设置和习惯需要一些时间和精力,但它非常强大。如果你从头开始,你可能会花费大量时间和精力做一些不太好的事情。
标签: junit javafx-2 javafx junit4 javafx-8