JavaFX - Upgrading from TestFX 3 to TestFX 4

2 min read
With TestFX 4 there are some things in comparison to release 3.1.2 which need to be changed when migrating to release 4.0.
I had some problems with the migration because I didn't find enough examples and my IDE suggested either no or the completely wrong imports for the methods like e.g. Matchers. I also found some erroneous examples of using TestFX 4. Some examples are using hamcrest matchers like assertThat("#loginButton", ...) which erroneously could result in lots of green bars, because these Matchers won't search the component with fx:id #loginButton. They are treating the first argument as String instead. Therefore: Take care of your imports in TestFX!
This post shows this parts and gives an overview of what has to be changed when planning a migration to the newest version. 



First the dependency has changed from:

<dependency>
    <groupId>org.loadui</groupId>
    <artifactId>testFx</artifactId>
    <version>3.1.2</version>
</dependency>

to two dependencies. The single TestFX dependency has been splitted in the core part of TestFX and the JUnit AddOn:

<dependency>
    <groupId>org.testfx</groupId>
    <artifactId>testfx-core</artifactId>
    <version>4.0.1-alpha</version
     <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testfx</groupId>
    <artifactId>testfx-junit</artifactId>
    <version>4.0.1-alpha</version>
    <scope>test</scope>
</dependency>

After that your test won't compile. You have to search and replace the Test class:
extends GuiTest becomes: extends ApplicationTest which is in package: org.testfx.framework.junit.
The overridden getRootNode() method has to be removed and its contained code has to be moved to the new start() method.

The last step is to change the methods used in the test. In comparison to the TestFX releases of 3.x there areother methods to interact with JavaFX components like Buttons, TextFields etc.
In the former releases all these methods like find() and verifyThat() where part of the extended GuiTest method. In release 4 this has changed.

Operations on Components like clickOn() or drag() are in class: org.testfx.api.FxRobot
Matchers like isNotNull() or isDisabled() can be found in: org.testfx.matcher.base
Asserts can be found in class: org.testfx.api.FxAssert

 Take care of your imports and have fun with the newest version of TestFX :-)


Bye,
Bennet


Be Social, Share!