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

浪尽此生 提交于 2021-02-07 20:35:04

问题


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?


回答1:


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)



回答2:


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...



来源:https://stackoverflow.com/questions/45109876/toolkit-not-initialized-exception-when-unit-testing-an-javafx-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!