You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TestFx无头模式报错:MonoclePlatformFactory未找到

Fixing "MonoclePlatformFactory not found" in JavaFX + Maven + TestFX Headless Tests

This error typically happens when Monocle dependencies aren't properly loaded, or critical JVM parameters aren't passed at the right time. Let's walk through targeted fixes based on your configuration:

1. Adjust Surefire Plugin Parameter Delivery

Right now, you're setting Monocle properties in systemPropertyVariables, but some JavaFX system properties need to be passed when the test JVM starts, not during test execution. Switch these to the argLine section to ensure they take effect immediately:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
        <threadCount>1</threadCount>
        <!-- Pass JVM args directly at launch -->
        <argLine>
            -Dglass.platform=Monocle 
            -Dmonocle.platform=Headless 
            -Dprism.order=sw
        </argLine>
        <additionalClasspathElements>
            <additionalClasspathElement>${java.home}/lib/ext/jfxrt.jar</additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
</plugin>

2. Remove Duplicate Property Config in Tests

Your ChronoTest class sets duplicate system properties that conflict with the Surefire plugin settings. Delete the redundant setup method to keep configuration consistent:

public class ChronoTest extends ApplicationTest {
    // Remove the setupHeadlessMode method entirely - all args are handled by Maven now
}

3. Verify Dependency Compatibility

Your openjfx-monocle version (1.8.0_20) matches Java 8, but updating TestFX to a more stable alpha version can resolve hidden compatibility issues:

<dependency>
    <groupId>org.testfx</groupId>
    <artifactId>testfx-core</artifactId>
    <version>4.0.16-alpha</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testfx</groupId>
    <artifactId>testfx-junit</artifactId>
    <version>4.0.16-alpha</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testfx</groupId>
    <artifactId>openjfx-monocle</artifactId>
    <version>1.8.0_20</version>
    <scope>test</scope>
</dependency>

4. Validate JFXRT Path

Double-check that ${java.home}/lib/ext/jfxrt.jar exists in your Java 8 JDK. If your JDK separates JavaFX, replace the classpath element with a system dependency:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>javafx</artifactId>
    <version>2.2</version>
    <scope>system</scope>
    <systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency>

Final Steps

Clean your Maven cache to ensure fresh dependencies are downloaded, then run tests:

mvn clean install -DskipTests
mvn test

内容的提问来源于stack exchange,提问作者JustinMartinDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:23:14