Hudson中的JUnit / Ant:如何在无头模式下开始测试?

时间:2014-06-21 18:32:50

标签: ant junit awt hudson headless

我在为Hudson的Netbeans开发的项目设置单元测试时遇到了麻烦。到目前为止,Hudson运行正常,监视我的git存储库,并自动启动构建。

但是当建筑完工时,哈德森开始进行单元测试。这就是问题所在:有些类使用awt,当从Hudson内部启动时,测试会失败:

[junit] Testcase: testParse_SimpleTextAndSymbols(com.dua3.util.text.LatexParserTest):   Caused an ERROR
[junit] Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
[junit] java.lang.InternalError: Can't connect to X11 window server using ':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:110)
[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:190)
[junit]     at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:102)
[junit]     at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
[junit]     at sun.awt.X11.XToolkit.<clinit>(XToolkit.java:119)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:190)
[junit]     at java.awt.Toolkit$2.run(Toolkit.java:868)
[junit]     at java.security.AccessController.doPrivileged(Native Method)
[junit]     at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:860)
[junit]     at sun.swing.SwingUtilities2.getSystemMnemonicKeyMask(SwingUtilities2.java:1877)
[junit]     at javax.swing.plaf.basic.BasicLookAndFeel.initComponentDefaults(BasicLookAndFeel.java:752)
[junit]     at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:434)
[junit]     at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:148)
[junit]     at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1589)
[junit]     at javax.swing.UIManager.setLookAndFeel(UIManager.java:536)
[junit]     at javax.swing.UIManager.setLookAndFeel(UIManager.java:576)
[junit]     at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1345)
[junit]     at javax.swing.UIManager.initialize(UIManager.java:1455)
[junit]     at javax.swing.UIManager.maybeInitialize(UIManager.java:1422)
[junit]     at javax.swing.UIManager.getLookAndFeelDefaults(UIManager.java:1034)
[junit]     at com.dua3.util.graph.javase.swing.SwingFont$SwingFontProvider.getDefaultFont(SwingFont.java:50)
[junit]     at com.dua3.util.graph.Font.getDefault(Font.java:52)
[junit]     at com.dua3.util.text.LatexParser$Settings.<init>(LatexParser.java:39)
[junit]     at com.dua3.util.text.LatexParser.parse(LatexParser.java:61)
[junit]     at com.dua3.util.text.LatexParserTest.testParse_SimpleTextAndSymbols(LatexParserTest.java:49)

我认为检查GraphicsEnvironment.isHeadless()应该足够了,但显然它不是。这是错误的代码片段:

if(!GraphicsEnvironment.isHeadless()) {
    final UIDefaults lookAndFeelDefaults = UIManager.getLookAndFeelDefaults(); // SwingFont.java:50
    if (lookAndFeelDefaults!=null) {
        swingFont = lookAndFeelDefaults.getFont("defaultFont"); 
    }
}

我在Hudson中试过以下内容:

  • 在&#34;构建&#34;中将Java选项设置为-Djava.awt.headless=true部分

  • 输入一行&#34; java.awt.headless = true&#34;在构建属性

所有这些都不起作用。似乎这些只传递给ant,但在调用java以运行JUnit测试时却没有传递。

有没有办法告诉蚂蚁我希望我的测试能在无头模式下运行?

1 个答案:

答案 0 :(得分:1)

我花了一些时间寻找这个,最后找到了answer。简而言之:在linux上设置java.awt.headless=true是不够的。你必须取消设置DISPLAY。因此,请转到“管理Hudson”,“配置系统”,确保在“全局属性”下选中“环境变量”并添加名称为DISPLAY的新变量(将值字段留空)。