Class ApplicationTest

  • All Implemented Interfaces:
    FxRobotInterface, ApplicationFixture

    public abstract class ApplicationTest
    extends FxRobot
    implements ApplicationFixture
    The base class that your JUnit test classes should extend from that interact with and/or verify the state of a JavaFX UI. Such test classes, containing one or more @Test-annotated methods (individual test cases), can interact with a JavaFX UI using the FxRobot methods that test class will inherit (as it extends ApplicationTest (this class) which extends FxRobot). Verifying the state of the UI can be accomplished by using either the Hamcrest based FxAssert.verifyThat(T, org.hamcrest.Matcher<? super T>) or the AssertJ based Assertions.assertThat(Node).

    Example:

    
     public class ColorSelectorTest extends ApplicationTest {
    
         Stage stage;
         ColorPicker colorPicker;
    
         {@literal @}Override
         public void start(Stage stage) throws Exception {
             this.stage = stage;
         }
    
        {@literal @}Before
         public void beforeEachTest() throws Exception {
             Platform.runLater(() -> {
                 colorPicker = new ColorPicker(Color.BLUE);
                 StackPane stackPane = new StackPane(colorPicker);
                 Scene scene = new Scene(root, 800, 800);
                 stage.setScene(scene);
                 stage.show();
             });
            WaitForAsyncUtils.waitForFxEvents();
         }
    
         {@literal @}Test
         public void shouldAllowUserToChangeColor() {
             // when:
             clickOn(colorPicker);
             type(KeyCode.DOWN);
             type(KeyCode.DOWN);
    
             // then:
             assertThat(colorPicker.getValue()).isEqualTo(Color.TEAL);
         }
     }
     
    • Constructor Detail

      • ApplicationTest

        public ApplicationTest()
    • Method Detail

      • launch

        public static void launch​(java.lang.Class<? extends javafx.application.Application> appClass,
                                  java.lang.String... appArgs)
                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • internalBefore

        public final void internalBefore()
                                  throws java.lang.Exception
        Throws:
        java.lang.Exception
      • internalAfter

        public final void internalAfter()
                                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • init

        public void init()
                  throws java.lang.Exception
        Specified by:
        init in interface ApplicationFixture
        Throws:
        java.lang.Exception
      • start

        public void start​(javafx.stage.Stage stage)
                   throws java.lang.Exception
        Specified by:
        start in interface ApplicationFixture
        Throws:
        java.lang.Exception
      • stop

        public void stop()
                  throws java.lang.Exception
        Specified by:
        stop in interface ApplicationFixture
        Throws:
        java.lang.Exception