I'm having some trouble getting TestFx working with Oracle's JavaFx HelloWorld app:
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
TestFx junit test:
class MyTest extends GuiTest {
public Parent getRootNode() {
return nodeUnderTest;
}
What should nodeUnderTest
be for this example?
TestFx is a unit test framework, so it is designed to grab parts of your GUI implementation and test on that. That requires you to make these parts available first and test targets (buttons etc) available by tagging them with ids.
getRootNode() provides the root for the following test procedures of GUI testing. In your example above the StackPane root might be a candidate... but that requires that you make that available for testing to allow:
So the app has to be modified to implement the getRoot(), returning the StackPane with its content for testing, not requiring the start() method.
The you are able to run tests on that...
There is also a simple way to test your entire application. To make sure that your app is properly initialised and started, it needs to be started by the JavaFX application launcher. Unfortunately, TestFX doesn't support this out of the box (at least I haven't found any way to do this) but you can easily do this by subclassing GuiTest: