Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Junit @rule for running tests on the JavaFX thread
import java.util.concurrent.CountDownLatch;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* A JUnit {@link Rule} for running tests on the JavaFX thread and performing
* JavaFX initialisation. To include in your test case, add the following code:
*
* <pre>
* {@literal @}Rule
* public JavaFXThreadingRule jfxRule = new JavaFXThreadingRule();
* </pre>
*
* @author Andy Till
*
*/
public class JavaFXThreadingRule implements TestRule {
/**
* Flag for setting up the JavaFX, we only need to do this once for all tests.
*/
private static boolean jfxIsSetup;
@Override
public Statement apply(Statement statement, Description description) {
return new OnJFXThreadStatement(statement);
}
private static class OnJFXThreadStatement extends Statement {
private final Statement statement;
public OnJFXThreadStatement(Statement aStatement) {
statement = aStatement;
}
private Throwable rethrownException = null;
@Override
public void evaluate() throws Throwable {
if(!jfxIsSetup) {
setupJavaFX();
jfxIsSetup = true;
}
final CountDownLatch countDownLatch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
statement.evaluate();
} catch (Throwable e) {
rethrownException = e;
}
countDownLatch.countDown();
}});
countDownLatch.await();
// if an exception was thrown by the statement during evaluation,
// then re-throw it to fail the test
if(rethrownException != null) {
throw rethrownException;
}
}
protected void setupJavaFX() throws InterruptedException {
long timeMillis = System.currentTimeMillis();
final CountDownLatch latch = new CountDownLatch(1);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// initializes JavaFX environment
new JFXPanel();
latch.countDown();
}
});
System.out.println("javafx initialising...");
latch.await();
System.out.println("javafx is initialised in " + (System.currentTimeMillis() - timeMillis) + "ms");
}
}
}
@mkarg

This comment has been minimized.

Copy link

mkarg commented Aug 13, 2016

Unfortunately does not work with @Test(timeout = ...) because JUnit apparently spans a new thread within baseStatement.evaluate(); ;-(

@NottmTony

This comment has been minimized.

Copy link

NottmTony commented Nov 30, 2016

Cheers for this ... worked a treat.

@dawidsamolyk

This comment has been minimized.

Copy link

dawidsamolyk commented Feb 3, 2017

In my opinion you should invoke countDownLatch.countDown(); in finally clause.
countDownLatch.await(); could be also dangerous. It would be better to use some timeout.

@Mrodent

This comment has been minimized.

Copy link

Mrodent commented Apr 20, 2020

I'm curious: does this do anything which TestFX does not do? With TestFX the approach is different, in that if you need things to run in the JavaFX thread you call Platform.runLater( new Runnable(){ ... , but after calling that you can go (in the non-JavaFX test thread) WaitForAsyncUtils.waitForFxEvents(); Maybe you wrote this utility before TestFX was widely used? Also I'm never sure of the processing "cost" of TestFX, whereas I can see that your setup is clearly a lightweight thing which will take minimal time to run. Also why use SwingUtilities.invokeLater() when you have the JavaFX equivalent Platform.runLater()?

@andytill

This comment has been minimized.

Copy link
Owner Author

andytill commented Apr 21, 2020

@Mrodent ha, using SwingUtilities.invokeLater() could be a bug but I can't remember now if there was a reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.