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?
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...
Add the following dependency to your project
<dependency>
<groupId>de.saxsys</groupId>
<artifactId>jfx-testrunner</artifactId>
<version>1.2</version>
</dependency>
and the following annotation to your test classes
@RunWith(JfxRunner.class)
Similar to @multiplayer1080 answer but more elegant...
import javafx.application.Platform
@BeforeAll
static void initJfxRuntime() {
Platform.startup(() -> {});
}
You can go further and declare an abstract class with this method like that:
abstract class FXTest {
@BeforeAll
static void initJfxRuntime() {
Platform.startup(() -> {});
}
}
and then inherit it when you test JavaFX runtime required stuff:
class SomeGuiTest extends FXTest {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With