StubbornShowaGuy
StubbornShowaGuy

Reputation: 269

JUnit failing from HeadlessException in program using java.awt/javax.swing

I have a Java project that I need to create a JUnit ant task and some tests for. The tests should not be dependent on the environment. The program that is to be tested uses a lot of GUI-related things from java.awt and javax.swing. I have tried various settings and modifications, but for some reason the test always dies from java.awt.HeadlessException. I have searched for a solution for this problem without luck - any help would be appreciated.

(I am using Java(TM) SE Runtime Environment (build 1.8.0_60-b27) on CentOS Linux release 7.2.1511 (Core))

I have added the code below to my 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>

This is the output of the JUnit test:

[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

And if I change headless in build.xml to false, it still fails but the result changes to this:

[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

Upvotes: 1

Views: 1426

Answers (1)

Arnaud
Arnaud

Reputation: 17534

It looks like the getMenuShortcutKeyMask from sun.awt.HeadlessToolkit, the Toolkit used when in headless mode (*), will always throw this Exception :

public int getMenuShortcutKeyMask()
        throws HeadlessException {
        throw new HeadlessException();
    }

This method is called by org.fife.ui.rtextarea.RTADefaultInputMap, so you won't be able to perform the tests as is.

(*)

Default Toolkit Creation

If a system property named java.awt.headless is set to true, then the headless implementation of Toolkit is used. Use the getDefaultToolkit() method of the Toolkit class to create an instance of headless toolkit:

Toolkit tk = Toolkit.getDefaultToolkit();

Upvotes: 2

Related Questions