我有一个Java项目,我需要创建一个JUnit ant任务和一些测试。
测试不应取决于环境。
要测试的程序使用了java.awt
和javax.swing
中许多与GUI相关的内容。
我尝试了各种设置和修改,但由于某种原因,测试总是死于java.awt.HeadlessException
。
我没有运气就找到了这个问题的解决方案 - 任何帮助都会受到赞赏。
(我在Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
上使用CentOS Linux release 7.2.1511 (Core)
)
我已将下面的代码添加到我的build.xml中:
<target name="test" depends="test-compile">
<junit>
<jvmarg value="-Djava.awt.headless=true"/>
<sysproperty key="java.awt.headless" value="true"/>
<classpath>
<path refid="junit.class.path"/>
<path refid="libs.class.path"/>
<pathelement location="${build.test.path}"/>
</classpath>
<batchtest>
<fileset dir="${build.test.path}">
<include name="**/*Test*"/>
</fileset>
</batchtest>
<formatter type="brief" usefile="false"/>
</junit>
</target>
这是JUnit测试的输出:
[junit] Test org.sampleprogram.MyAppTest FAILED
[junit] Testsuite: org.sampleprogram.MySomethingTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.311 sec
[junit]
[junit] Testcase: testSample(org.sampleprogram.MySomethingTest): Caused an ERROR
[junit] null
[junit] java.lang.ExceptionInInitializerError
[junit] at org.sampleprogram.MySomething.initStaticProperties(Unknown Source)
[junit] at org.sampleprogram.MySomething.<clinit>(Unknown Source)
[junit] at org.sampleprogram.MySomethingTest.testSample(Unknown Source)
[junit] Caused by: java.awt.HeadlessException
[junit] at sun.awt.HeadlessToolkit.getMenuShortcutKeyMask(HeadlessToolkit.java:236)
[junit] at org.fife.ui.rtextarea.RTADefaultInputMap.getDefaultModifier(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTADefaultInputMap.<init>(Unknown Source)
[junit] at org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaDefaultInputMap.<init>(Unknown Source)
[junit] at org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaUI.getRTextAreaInputMap(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTextAreaUI.installKeyboardActions(Unknown Source)
[junit] at javax.swing.plaf.basic.BasicTextUI.installUI(BasicTextUI.java:805)
[junit] at org.fife.ui.rtextarea.RTextAreaUI.installUI(Unknown Source)
[junit] at javax.swing.JComponent.setUI(JComponent.java:666)
[junit] at javax.swing.text.JTextComponent.setUI(JTextComponent.java:328)
[junit] at org.fife.ui.rtextarea.RTextAreaBase.setRTextAreaUI(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTextAreaBase.init(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTextArea.init(Unknown Source)
[junit] at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.init(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTextAreaBase.<init>(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTextArea.<init>(Unknown Source)
[junit] at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.<init>(Unknown Source)
[junit] at org.sampleprogram.gui.TextArea.<init>(Unknown Source)
[junit] at org.sampleprogram.MyApp.<clinit>(Unknown Source)
[junit]
[junit]
[junit] Test org.sampleprogram.MySomethingTest FAILED
如果我将headless
中的build.xml
更改为false
,它仍会失败,但结果会更改为:
[junit] Testsuite: org.sampleprogram.MyAppTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.471 sec
[junit]
[junit] Testcase: testSample(org.sampleprogram.MyAppTest): Caused an ERROR
[junit] Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
[junit] java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
[junit] at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
[junit] at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
[junit] at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:115)
[junit] at java.security.AccessController.doPrivileged(Native Method)
[junit] at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:74)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:264)
[junit] at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
[junit] at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
[junit] at javax.swing.RepaintManager.<clinit>(RepaintManager.java:233)
[junit] at javax.swing.JComponent.repaint(JComponent.java:4792)
[junit] at java.awt.Component.repaint(Component.java:3313)
[junit] at javax.swing.text.JTextComponent.setEditable(JTextComponent.java:1758)
[junit] at javax.swing.text.JTextComponent.<init>(JTextComponent.java:309)
[junit] at javax.swing.JTextArea.<init>(JTextArea.java:204)
[junit] at javax.swing.JTextArea.<init>(JTextArea.java:140)
[junit] at org.fife.ui.rtextarea.RTextAreaBase.<init>(Unknown Source)
[junit] at org.fife.ui.rtextarea.RTextArea.<init>(Unknown Source)
[junit] at org.fife.ui.rsyntaxtextarea.RSyntaxTextArea.<init>(Unknown Source)
[junit] at org.sampleprogram.gui.SampleTextArea.<init>(Unknown Source)
[junit] at org.sampleprogram.MyApp.<clinit>(Unknown Source)
[junit] at org.sampleprogram.MyApp.initStaticProperties(Unknown Source)
[junit] at org.sampleprogram.MyApp.<clinit>(Unknown Source)
[junit] at org.sampleprogram.MyAppTest.testSample(Unknown Source)
[junit]
[junit]
[junit] Test org.sampleprogram.MyAppTest FAILED
答案 0 :(得分:1)
看起来sun.awt.HeadlessToolkit
中的getMenuShortcutKeyMask,headless模式(*)中使用的Toolkit
,始终会抛出此异常:
public int getMenuShortcutKeyMask()
throws HeadlessException {
throw new HeadlessException();
}
此方法由org.fife.ui.rtextarea.RTADefaultInputMap调用,因此您将无法按原样执行测试。
(*)
默认工具包创建
如果名为java.awt.headless的系统属性设置为true,那么 使用Toolkit的无头实现。使用 用于创建实例的Toolkit类的getDefaultToolkit()方法 无头工具包:
Toolkit tk = Toolkit.getDefaultToolkit();