Getting started with TestFx

故事扮演 提交于 2019-12-06 12:05:27

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:

 class MyTest extends GuiTest {
     public Parent getRootNode() {
         HelloWorld app = new HelloWorld();
         return app.getRoot(); // the root StackPane with button
     }
 }

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

@Test
public void testButtonClick(){
    final Button button = find("#button"); // requires your button to be tagged with setId("button")
    click(button);
    // verify any state change expected on click.
}

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:

public class HelloWorldGuiTest extends GuiTest {
  private static final SettableFuture<Stage> stageFuture = SettableFuture.create();

  protected static class TestHelloWorld extends HelloWorld {
    public TestHelloWorld() {
      super();
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
      super.start(primaryStage);
      stageFuture.set(primaryStage);
    }
  }

  @Before
  @Override
  public void setupStage() throws Throwable {
    assumeTrue(!UserInputDetector.instance.hasDetectedUserInput());

    FXTestUtils.launchApp(TestHelloWorld.class); // You can add start parameters here
    try {
      stage = targetWindow(stageFuture.get(25, TimeUnit.SECONDS));
      FXTestUtils.bringToFront(stage);
    } catch (Exception e) {
      throw new RuntimeException("Unable to show stage", e);
    }
  }

  @Override
  protected Parent getRootNode() {
    return stage.getScene().getRoot();
  }

  // Add your tests here

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!