我在为Hudson的Netbeans下开发的项目设置单元测试时遇到了麻烦。到目前为止,Hudson运行良好,监视我的git存储库,并自动开始构建。
但是当构建完成时,Hudson开始单元测试。问题就在这里:有些类使用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中尝试了以下内容:
在"Build"部分设置Java选项为
-Djava.awt.headless=true
输入一行"java.awt. net "。Build属性
headless=true
所有这些都不起作用。似乎这些只传递给ant
,但在调用java
运行JUnit测试时不会继续。
是否有任何方法可以告诉ant我希望我的测试以headless模式运行?
我花了一些时间寻找这个,终于找到了答案。简而言之:在linux上设置java.awt.headless=true
是不够的。你必须取消设置DISPLAY
。因此,进入"Manage Hudson","Configure System",确保在"Global Properties"下勾选"Environment variables",并添加一个名为DISPLAY
的新变量(保留值字段空白)。