Skip to content

Testing JavaFX Applications

roy shchory edited this page Mar 25, 2017 · 2 revisions

TestFx

A Javafx testing library (also used by oracle when testing there new Javafx gui models - http://www.guigarage.com/2015/02/test-driven-development-youtube/ if you believe the people trying to sell you the library) provides a simple api to simulate actual human interaction with the application.

Getting TestFX

To start using TestFX, simply add the following elements to your pom.xml file:

<dependency>
    <groupId>org.loadui</groupId>
    <artifactId>testFx</artifactId>
    <version>3.1.0</version>
</dependency>

Setup

We start by creating a new class that extends GuiTest. Since GuiTest is abstract, our IDE will prompt us to implement the abstract method getRootNode:

public class LoginTest extends GuiTest
{
   public Parent getRootNode()
   {
      //TODO
   }
}

Creating tests

This is generally how your TestFX test classes will look like:

class MyTest extends GuiTest {
  public Parent getRootNode() {
    return nodeUnderTest;
  }

  @Test
  public static void nodeUnderTest_should_behaveInAParticularWay() {
    // Test logic goes here.
  }
}

Api

Here are the manuals specifing the TestFx api:

Examples

Here are some examples from the Testfx github wiki:

Clone this wiki locally