Class JavaFXInterceptorUtils.JavaFxInterceptor

java.lang.Object
org.testfx.framework.junit5.JavaFXInterceptorUtils.JavaFxInterceptor
All Implemented Interfaces:
org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.InvocationInterceptor
Enclosing class:
JavaFXInterceptorUtils

public static class JavaFXInterceptorUtils.JavaFxInterceptor extends Object implements org.junit.jupiter.api.extension.InvocationInterceptor
Simple JUnit 5 extension to ensure that @Test statements are executed in the JavaFX UI thread. This is (strictly) necessary when testing setter and/or getter methods of JavaFX classes (ie. Node derived, properties etc).

Example usage:

 
 @ExtendWith(ApplicationExtension.class)
 @ExtendWith(JavaFxInterceptor.class)
 public class SquareButtonTest {
     @Start
     public void start(Stage stage) {
         // usual FX initialisation
         // ...
     }

    @TestFx
    // note: this is equivalent to @Test when using @ExtendWith(JavaFxInterceptor.class)
    public void testJavaFxThreadSafety() {
        // verifies that this test is indeed executed in the JavaFX thread
        assertTrue(Platform.isFxApplicationThread());

        // perform the regular JavaFX thread safe assertion tests
        // ...
    }

    @Test // also executed in JavaFX thread,
    // for different behaviour use:  @ExtendWith(SelectiveJavaFxInterceptor.class)
    public void testNonJavaFx() {
        // verifies that this test is also executed in the JavaFX thread
        assertTrue(Platform.isFxApplicationThread());

        // perform regular assertion tests within the JavaFX thread
        // ...
    }
 }

 
 
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor

    org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<T>
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    interceptTestMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<Void> invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext<Method> invocationContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor

    interceptAfterAllMethod, interceptAfterEachMethod, interceptBeforeAllMethod, interceptBeforeEachMethod, interceptDynamicTest, interceptDynamicTest, interceptTestClassConstructor, interceptTestFactoryMethod, interceptTestTemplateMethod
  • Constructor Details

    • JavaFxInterceptor

      public JavaFxInterceptor()
  • Method Details

    • interceptTestMethod

      public void interceptTestMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<Void> invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext<Method> invocationContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Throwable
      Specified by:
      interceptTestMethod in interface org.junit.jupiter.api.extension.InvocationInterceptor
      Throws:
      Throwable