'Toolkit not initialized' exception when unit-testing an JavaFX application

Viewed 4377

When I try to perform unit tests on components which contain JavaFX controls I get a java.lang.IllegalStateException: Toolkit not initialized.

How can components be unit tested which operate with JavaFX controls?

3 Answers

Just declare and initialize a JFX Panel. Like:

@Test
public void test() throws Exception {
    JFXPanel fxPanel = new JFXPanel();
    [.. Begin tests ..]
}

It is the easy way...

Similar to @multiplayer1080 answer but more elegant...

import javafx.application.Platform

@BeforeAll
static void initJfxRuntime() {
    Platform.startup(() -> {});
}
Related