javaunit-testingjavafx-8

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


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?


Solution

  • 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)