Skip to content
Manuel Mauky edited this page Sep 9, 2015 · 1 revision

The JfxRunner is a JUnit-Runner which provides a JavaFX application thread for unit tests.

The JfxRunner was first created as external lib and later integrated in the mvvmfx-testing-utils module. The idea is based on a blog post by Andy White.

Usage

To use the JfxRunner you have to use the @RunWith annotation provided by JUnit with the argument JfxRunner.class like this:

import org.junit.runner.RunWith;
import org.junit.Test

import de.saxsys.mvvmfx.testingutils.jfxrunner.JfxRunner

@RunWith(JfxRunner.class)
public class MyTest {

    @Test
    public void test() {
        new TestField(); // controls can only be created with a running JavaFX application thread.
    }
}

The @RunWith(JfxRunner.class) annotation results in a running JavaFX application thread in the background. This way you can create and use JavaFX Controls in your test method.

Notice that the test method itself is not executed on the JavaFX thread:

@RunWith(JfxRunner.class)
public class MyTest {

    @Test
    public void test() {
        assertFalse(Platform.isFxApplicationThread());
    }
}

To run a test method on the JavaFX application thread you can use the @TestInJfxThread annotation like this:

@RunWith(JfxRunner.class)
public class MyTest {

    @Test
    @TestInJfxThread
    public void test() {
        assertTrue(Platform.isFxApplicationThread());
    }
}