JavaFX 测试在 Windows 中失败,但在 OSX 和 Linux 中工作

标签 java linux windows macos javafx

我的 JavaFX 测试仅在 Windows 中失败,但在 OSX 和 Linux 中运行良好

这是堆栈跟踪:

Edit : 1

Running com.myproject.meditor.SLIVCodeAreaTest
Tests run: 17, Failures: 0, Errors: 17, Skipped: 0, Time elapsed: 6.656 sec <<< FAILURE! - in com.myproject.meditor.SLIVCodeAreaTest
testRHBlock(com.myproject.meditor.SLIVCodeAreaTest)  Time elapsed: 0 sec  <<< ERROR!
java.lang.ExceptionInInitializerError: null
    at com.sun.glass.ui.Screen.getScreens(Screen.java:70)
    at com.sun.glass.ui.Screen.getMainScreen(Screen.java:61)
    at com.sun.javafx.font.PrismFontFactory.getSystemFontSize(PrismFontFactory.java:1911)
    at com.sun.javafx.font.PrismFontLoader.getSystemFontSize(PrismFontLoader.java:240)
    at javafx.scene.text.Font.getDefaultSystemFontSize(Font.java:71)
    at javafx.scene.text.Font.getDefault(Font.java:86)
    at javafx.scene.text.Text.getFont(Text.java:478)
    at javafx.scene.text.Text.getFontInternal(Text.java:487)
    at javafx.scene.text.Text.access$100(Text.java:95)
    at javafx.scene.text.Text$1.getFont(Text.java:206)
    at com.sun.javafx.text.GlyphLayout.breakRuns(GlyphLayout.java:174)
    at com.sun.javafx.text.PrismTextLayout.buildRuns(PrismTextLayout.java:770)
    at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1021)
    at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:223)
    at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:246)
    at javafx.scene.text.TextFlow.computePrefWidth(TextFlow.java:203)
    at javafx.scene.Parent.prefWidth(Parent.java:915)
    at javafx.scene.layout.Region.prefWidth(Region.java:1419)
    at org.fxmisc.richtext.ParagraphBox.computePrefWidth(ParagraphBox.java:199)
    at org.fxmisc.richtext.ParagraphBox.computeMinWidth(ParagraphBox.java:191)
    at javafx.scene.Parent.minWidth(Parent.java:943)
    at javafx.scene.layout.Region.minWidth(Region.java:1383)
    at org.fxmisc.flowless.VerticalHelper.minBreadth(OrientationHelper.java:234)
    at org.fxmisc.flowless.OrientationHelper.minBreadth(OrientationHelper.java:44)
    at org.reactfx.collection.MappedList.get(MappedList.java:27)
    at org.reactfx.collection.MemoizationListImpl.force(MemoizationList.java:121)
    at org.fxmisc.flowless.SizeTracker.breadthFor(SizeTracker.java:161)
    at org.fxmisc.flowless.CellPositioner.getSizedCell(CellPositioner.java:130)
    at org.fxmisc.flowless.CellPositioner.placeStartAt(CellPositioner.java:95)
    at org.fxmisc.flowless.Navigator.placeStartAtMayCrop(Navigator.java:183)
    at org.fxmisc.flowless.Navigator.visit(Navigator.java:111)
    at org.fxmisc.flowless.StartOffStart.accept(TargetPosition.java:49)
    at org.fxmisc.flowless.Navigator.layoutChildren(Navigator.java:67)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at org.fxmisc.flowless.VirtualFlow.layoutChildren(VirtualFlow.java:165)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at org.fxmisc.flowless.VirtualFlow.visibleCells(VirtualFlow.java:138)
    at com.myproject.util.javafx.codearea.SEditor.<init>(SEditor.java:659)
    at com.myproject.util.javafx.codearea.SEditor.<init>(SEditor.java:607)
    at com.myproject.util.javafx.codearea.SCodeArea.<init>(SCodeArea.java:122)
    at com.myproject.meditor.SLICodeArea.<init>(SLICodeArea.java:124)
    at com.myproject.meditor.SLICodeArea.<init>(SLICodeArea.java:114)
    at com.myproject.meditor.SLItemCodeArea.<init>(SLICodeArea.java:106)
    at com.myproject.meditor.SLItemCodeArea.<init>(SLICodeArea.java:103)
    at com.myproject.meditor.SLIVCodeAreaTest.<clinit>(SLIVCodeAreaTest.java:14)

EDIT: 2

从堆栈跟踪中,触发异常的代码部分(SLIVCodeAreaTest.java:14)位于代码部分的private static final SLIVCodeArea声明中:

import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class SLIVCodeAreaTest {     
    private static final SLIVCodeArea sEditor = new SLIVCodeArea(new SLICodeArea()) {

        @Override protected void updateVisibleParagraphRange() {}   };

    private static final VEditorProblemList sProblemList = 
        new VEditorProblemList(sEditor, new SimpleObjectProperty<>(), false);

    static { sEditor.aggregatedTextChanges().setAggregationEnabled(false); }

Edit : 3

如果我从上面的初始化部分删除关键字static,错误的堆栈跟踪将变为:

Running com.myproject.meditor.SLIVCodeAreaTest
Tests run: 17, Failures: 0, Errors: 17, Skipped: 0, Time elapsed: 1.124 sec <<< FAILURE! - in Running com.myproject.meditor.SLIVCodeAreaTest
testRHBlock(com.myproject.meditor.SLIVCodeAreaTest)  Time elapsed: 0 sec  <<< ERROR!
java.lang.RuntimeException: Internal graphics not initialized yet
    at com.sun.glass.ui.Screen.getScreens(Screen.java:70)
    at com.sun.glass.ui.Screen.getMainScreen(Screen.java:61)
    at com.sun.javafx.font.PrismFontFactory.getSystemFontSize(PrismFontFactory.java:1911)
    at com.sun.javafx.font.PrismFontLoader.getSystemFontSize(PrismFontLoader.java:240)
    at javafx.scene.text.Font.getDefaultSystemFontSize(Font.java:71)
    at javafx.scene.text.Font.getDefault(Font.java:86)
    at javafx.scene.text.Text.getFont(Text.java:478)
    at javafx.scene.text.Text.getFontInternal(Text.java:487)
    at javafx.scene.text.Text.access$100(Text.java:95)
    at javafx.scene.text.Text$1.getFont(Text.java:206)
    at com.sun.javafx.text.GlyphLayout.breakRuns(GlyphLayout.java:174)
    at com.sun.javafx.text.PrismTextLayout.buildRuns(PrismTextLayout.java:770)
    at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1021)
    at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:223)
    at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:246)
    at javafx.scene.text.TextFlow.computePrefWidth(TextFlow.java:203)
    at javafx.scene.Parent.prefWidth(Parent.java:915)
    at javafx.scene.layout.Region.prefWidth(Region.java:1419)
    at org.fxmisc.richtext.ParagraphBox.computePrefWidth(ParagraphBox.java:199)
    at org.fxmisc.richtext.ParagraphBox.computeMinWidth(ParagraphBox.java:191)
    at javafx.scene.Parent.minWidth(Parent.java:943)
    at javafx.scene.layout.Region.minWidth(Region.java:1383)
    at org.fxmisc.flowless.VerticalHelper.minBreadth(OrientationHelper.java:234)
    at org.fxmisc.flowless.OrientationHelper.minBreadth(OrientationHelper.java:44)
    at org.reactfx.collection.MappedList.get(MappedList.java:27)
    at org.reactfx.collection.MemoizationListImpl.force(MemoizationList.java:121)
    at org.fxmisc.flowless.SizeTracker.breadthFor(SizeTracker.java:161)
    at org.fxmisc.flowless.CellPositioner.getSizedCell(CellPositioner.java:130)
    at org.fxmisc.flowless.CellPositioner.placeStartAt(CellPositioner.java:95)
    at org.fxmisc.flowless.Navigator.placeStartAtMayCrop(Navigator.java:183)
    at org.fxmisc.flowless.Navigator.visit(Navigator.java:111)
    at org.fxmisc.flowless.StartOffStart.accept(TargetPosition.java:49)
    at org.fxmisc.flowless.Navigator.layoutChildren(Navigator.java:67)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at org.fxmisc.flowless.VirtualFlow.layoutChildren(VirtualFlow.java:165)
    at javafx.scene.Parent.layout(Parent.java:1087)
    at org.fxmisc.flowless.VirtualFlow.visibleCells(VirtualFlow.java:138)
    at com.myproject.util.javafx.codearea.SEditor.<init>(SEditor.java:659)
    at com.myproject.util.javafx.codearea.SEditor.<init>(SEditor.java:607)
    at com.myproject.util.javafx.codearea.SCodeArea.<init>(SCodeArea.java:122)
    at com.myproject.meditor.SLICodeArea.<init>(SLICodeArea.java:124)
    at com.myproject.meditor.SLICodeArea.<init>(SLICodeArea.java:114)
    at com.myproject.meditor.SLICodeArea.<init>(SLICodeArea.java:106)
    at com.myproject.meditor.SLICodeArea.<init>(SLICodeArea.java:103)
    at com.myproject.meditor.SLIVCodeAreaTest.<init>(SLIVCodeAreaTest.java:14)

PS : The test worked in OSX with and without static keyword.

相同的代码在 Linux 和 MacOS 中运行良好,但在 Windows 中失败。 有人知道为什么吗?

最佳答案

JavaFXJUnit 和任何 java.lang.ExceptionInInitializerError 的组合是抑制异常 Toolkit 未初始化 发生的指示符。

要初始化 JavaFX,您可以等待 new JFXPanel() 完成,然后再运行单元测试。

参见:

关于JavaFX 测试在 Windows 中失败,但在 OSX 和 Linux 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46026733/

相关文章:

java - Android 按钮方向垂直宽度 100%

linux - 脚本中奇怪的 grep 行为

linux - 关于 shell 和子 shell

linux - 更改 xterm 为组合键生成的转义序列

windows - 在 Windows 和 Linux/Ubuntu 上运行的基准测试软件

Java-Spring,奇怪的NullPointerException

java - 是否有(自动)备份 Hudson CI 文件的方法?

windows - 使用 NativeActivity 制作 Android NDK 应用程序?

windows - git - 服务器主机 key 未缓存

Java输出: can't write to subdirectory