FX-Test is a library to implement JUnit-Test for JavaFX applications
FX-Test is similar to TestFX in its purpose but there are multiple reasons why FX-Test has been created:
- There are License problems hence TestFX can not be used in Eclipse.org projects like e(fx)clipse
- Test-FX' bootstrap process is not compatible with e4 OSGi-Applications (with some trickery you can get it running)
While the 2nd problem could most likely get addressed by contributing to TestFX the first one is an unresolvable blocker and so we are NOT suffering from NIHS.
To test components the simplest way is to subclass at.bestsolution.fx.test.junit.FXComponentTest
and implement its createComponent
method.
Let's suppose you have a LoginComponent
like this:
public class LoginComponent {
private TextField username;
private PasswordField password;
private Button login;
public LoginComponent(BorderPane parent) {
// ....
}
}
You create a subclass of FXComponentTest
like this:
public class LoginComponentTest extends FXComponentTesty<BorderPane,LoginComponent> {
public LoginComponentTest() {
super(BorderPane.class);
}
@Override
protected LoginComponent createComponent(BorderPane parent) {
return new LoginComponent(parent);
}
}
And start writing you test methods like this:
@Test
public void simpleLoginTest() {
rcontroller().cssFirst(".text-field").get().typeText("[email protected]");
rcontroller().cssFirst(".password-field").get().typeText("test");
rcontroller().cssFirst(".button").get().click();
}
Finally you need to make a choice:
- You run the complete Test-Class with a specialized JUnit-Runner named
at.bestsolution.fx.test.junit.FXRunner
- You annotate your Test-Methods with
@at.bestsolution.fx.test.junit.FXTest
In general we recommend using the FXRunner
but eg if you want to use another JUnit-Runner like org.junit.runners.Parameterized
the FXTest-Rule is be a handy thing.
See JUnit-Samples for monkey-see-monkey-do example code.
Usage in a maven build requires you to add the an extra repository because there are dependencies on e(fx)clipse projects currently not found in maven-central.
So first step is to add:
<repositories>
<repository>
<name>BestSolution e(fx)clipse releases</name>
<id>efxclipse-releases</id>
<url>http://maven.bestsolution.at/efxclipse-releases/</url>
</repository>
</repositories>
and now you can add:
<dependency>
<groupId>at.bestsolution.fx.test</groupId>
<artifactId>at.bestsolution.fx.test.junit</artifactId>
<version>0.0.1</version>
<scope>test</scope>
</dependency>
If you use the standard-layout proposed by the e(fx)clipse development team your application structure should look like this:
my.app
my.app.feature
my.app.product
my.app.releng
To start adding JUnit-Tests you create a 5th project my.app.junit
and add a pom.xml like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>My e4 app - app junit bundle</name>
<groupId>my.app</groupId>
<artifactId>my.app.junit</artifactId>
<packaging>eclipse-test-plugin</packaging>
<parent>
<groupId>my.app</groupId>
<artifactId>my.app.releng</artifactId>
<relativePath>../my.app.releng/pom.xml</relativePath>
<version>1.0.0-SNAPSHOT</version>
</parent>
<build>
<resources>
<resource>
<directory>.</directory>
<includes>
<include>META-INF/</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<argLine>-Dorg.osgi.framework.bundle.parent=ext</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<dependency-resolution>
<extraRequirements>
<requirement>
<type>eclipse-feature</type>
<id>org.eclipse.fx.runtime.e4fx.feature</id>
<versionRange>0.0.0</versionRange>
</requirement>
<requirement>
<type>eclipse-feature</type>
<id>my.app.feature</id>
<versionRange>0.0.0</versionRange>
</requirement>
</extraRequirements>
</dependency-resolution>
</configuration>
</plugin>
</plugins>
</build>
</project>
To implement the JUnit-Test you subclass at.bestsolution.fx.test.e4.junit.E4JunitTestCase
like this:
@RunWith(FXRunner.class)
class ApplicationTest extends E4JunitTestCase {
public ApplicationTest() {
super("my.app.product");
}
}
Now you can start implementing your test methods. See JUnit-Sample for monkey-see-monkey-do example code.
You can grab binary SNAPSHOTS:
- for maven driven applications NONE (you need to build from source)
- for OSGi/PDE/maven-tycho driven applications from http://downloads.foss.bestsolution.at/fx-test/snapshots/repository/
- 0.0.1
- Maven: Use maven central
- OSGi/PDE/tycho: http://downloads.foss.bestsolution.at/fx-test/releases/0.0.1/repository/