Class JavaFXInterceptorUtils.SelectiveJavaFxInterceptor

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.InvocationInterceptor
    Enclosing class:
    JavaFXInterceptorUtils

    public static class JavaFXInterceptorUtils.SelectiveJavaFxInterceptor
    extends java.lang.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(SelectiveJavaFxInterceptor.class)
     public class SquareButtonTest {
         @Start
         public void start(Stage stage) {
             // usual FX initialisation
             // ...
         }
    
        @TestFx // forces execution in JavaFX thread
        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 // explicitly not executed in JavaFX thread;
        // for different behaviour use:  @ExtendWith(JavaFxInterceptor.class)
        public void testNonJavaFx() {
            // verifies that this test is not executed within the JavaFX thread
            assertFalse(Platform.isFxApplicationThread());
    
            // perform the regular non-JavaFX thread-related assertion tests
            // ...
        }
     }
    
     
     
    • Nested Class Summary

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

        org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<T extends java.lang.Object>
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void interceptTestMethod​(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<java.lang.Void> invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext<java.lang.reflect.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 Detail

      • SelectiveJavaFxInterceptor

        public SelectiveJavaFxInterceptor()
    • Method Detail

      • interceptTestMethod

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