Thursday 4 October 2012

JUnit Rule for JavaFX Controller Testing

Testing JavaFX controllers is relatively easy although you do have to jump through a couple of hoops. Firstly, JavaFX must be initialised and secondly JavaFX controls must only be mutated on the JavaFX thread created internally during initialisation. The JUnit rule below will setup JavaFX once and run all tests on the FX thread. All you need to do is create it as a field in your test case class and annotate it with @org.junit.Rule. You will need to be running JUnit 4.9 to use the rule API.