I have a small project for demonsration JavaFX + TestFX under maven. I use:
Failed to load Glass factory class
I used to have that error too, in my case i manually build the Monocle .jar and copied it to the JVM (jre/lib/ext).
Build Monocle according to the documentation on https://github.com/TestFX/Monocle, alternatively build it from the OpenJDK repository.
So the following worked for me:
-Dtestfx.robot=glass -Dglass.platform=Monocle -Dmonocle.platform=Headless -Dprism.order=sw
The -Dtestfx.robot=glass
is necessary, because TestFX otherwise uses the AWTRobot, which moves your mouse using the system event queue. Maybe that's enough of addition for your test to work properly.
Looking at your pom.xml
, junit brings it's own version of hamcrest-core
, so you should exclude that to avoid issues ( if you want to import several hamcrest matchers ). I used to have errors using both.
For further research, the link of which @ItachiUchiha provided and the post on https://zentrieredich.wordpress.com/2014/12/23/javafx-testen-mit-monocle/ helped me going the way with TestFX using Monocle.
To get rid of with
java.lang.ClassNotFoundException: com.sun.glass.ui.monocle.MonoclePlatformFactory
instead of copy the Monocle .jar to the JVM (jre/lib/ext)), we can add .jar to our project and load the MonoclePlatformFactory class in runtime to the classpath like below:
static {
try {
File AGENT_JAR = new File("../lib/openjfx-monocle-1.8.0_20.jar");
Method addUrl = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addUrl.setAccessible(true);
addUrl.invoke(PlatformFactory.class.getClassLoader(), AGENT_JAR.toURI().toURL());
} catch (Exception e) {
e.printStackTrace();
}
}
I would recommend to use TestFX 3.x because 4.x is still in alpha version.
A very nice blog about how to test your application and the working of TestFX is given in :
TestFX internals explained
For monocle usage, you can follow :
Headless UI Testing with TestFX and JavaFX 8