Created
November 18, 2015 10:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* © 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> | |
* @RunWith( JfxTestRunner.class ) | |
* public class MyUnitTest | |
* { | |
* @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