Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Created November 18, 2015 10:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bugabinga/a2703bfce0ca842d0e38 to your computer and use it in GitHub Desktop.
Save bugabinga/a2703bfce0ca842d0e38 to your computer and use it in GitHub Desktop.
This runner can be used to run JUnit-Tests on the JavaFx-Thread.
/**
* © 2015 isp-insoft all rights reserved.
*/
package com.isp.lpt;
import java.util.concurrent.CountDownLatch;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import com.sun.javafx.application.PlatformImpl;
/**
* This runner can be used to run JUnit-Tests on the JavaFx-Thread. This class can be used as a parameter to
* the {@link RunWith} annotation. Example: *
*
* <pre>
* <code>
* &#64;RunWith( JfxTestRunner.class )
* public class MyUnitTest
* {
* &#64;Test
* public void testMyMethod()
* {
* //...
* }
* }
* </code>
* </pre>
*
* @author okr
* @date 18.11.2015
*
*/
@SuppressWarnings( "restriction" )
public class JfxTestRunner extends BlockJUnit4ClassRunner
{
/**
* Creates a test runner, that initializes the JavaFx runtime.
*
* @param klass The class under test.
* @throws InitializationError if the test class is malformed.
*/
public JfxTestRunner( final Class<?> klass ) throws InitializationError
{
super( klass );
try
{
setupJavaFX();
}
catch ( final InterruptedException e )
{
throw new InitializationError( "Could not initialize the JavaFx platform." );
}
}
private static void setupJavaFX() throws InterruptedException
{
final CountDownLatch latch = new CountDownLatch( 1 );
// initializes JavaFX environment
PlatformImpl.startup( () ->
{
/* No need to do anything here */
} );
latch.countDown();
latch.await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment