JavaFX + maven + TestFX + monocle don't work together

后端 未结 3 1038
日久生厌
日久生厌 2020-12-21 13:18

I have a small project for demonsration JavaFX + TestFX under maven. I use:

  1. Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
  2. Apache Maven 3.2.5 (1
相关标签:
3条回答
  • 2020-12-21 13:42

    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:

    • Copy the Monocle .jar you build to jre/lib/ext of your JDK.
    • Run your application with the following JVM arguments:
    -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.

    0 讨论(0)
  • 2020-12-21 13:48

    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();
                }
            }
    
    0 讨论(0)
  • 2020-12-21 13:57

    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

    0 讨论(0)
提交回复
热议问题