Hello
I have a class that starts a JavaFX Dialog (out of a Swing Action). The dialog creation is wrapped into a call to Platform.runLater(Runnable).
I have this alredy working.
Now I'm creating a UnitTest which checks that the DialogCreation method is called.
The problem is, that within my unittest the Runnable is called after the testmethod finised. Therefore the test cannot recognize the call to my dialog cration method.
here is a SSCCE:
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.junit.Rule;
import org.junit.Test;
import javafx.application.Platform;
/**
* test fails because Platform.runLater() is invokes after test has ended
* @author TPD-opitz
*/
public class FxDialogTest {
public static class FxDialogOpenAction extends AbstractAction {
private final DialogCreatorInterface _dialogCreatorInterface;
public FxDialogOpenAction(DialogCreatorInterface dialogCreatorInterface) {
_dialogCreatorInterface = dialogCreatorInterface;
}
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("in starter thread, could be Swing");
Platform.runLater(() -> {
System.out.println("in FX thread, but too late");
_dialogCreatorInterface.createFxDialog();
});
}
}
public static interface DialogCreatorInterface {
void createFxDialog();
}
@Rule
public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();
@Test
public void testActionPerformed_noPrecondition__callsCreateDialog() throws Exception {
DialogCreatorInterface dialogCreatorInterface = mock(DialogCreatorInterface.class);
Action action = new FxDialogOpenAction(dialogCreatorInterface);
action.actionPerformed(mock(ActionEvent.class));
Thread.sleep(2000);
System.out.println("in tester thread after wait");
verify(dialogCreatorInterface).createFxDialog();
}
}
and the custom Rule used by the test:
import java.util.concurrent.CountDownLatch;
import javax.swing.SwingUtilities;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
/**
* 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() {
@Override
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");
}
}
}
the output is:
javafx initialising...
javafx is initialised in 200ms
in starter thread, could be Swing
in tester thread after wait
in FX thread, but too late
how do I force the FX-event loop to be invokes in time?
bye
TPD