【问题标题】:Why my unit test doesn't cover gui component generation code?为什么我的单元测试不包括 gui 组件生成代码?
【发布时间】:2015-06-04 19:56:32
【问题描述】:

我正在尝试对生成 GUI 组件的代码进行单元测试。

这是我的测试代码。

@Test
public void testMainFrame() {
    mFrame = new MainFrame();

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        fail("MainFrame Test Fail");
    }

    assertTrue(true);
}

目标代码在这里。

public MainFrame() {
    super("Title");

    /* Main page */
    clayout = new CardLayout();
    headerPane = new JPanel(clayout);
    statusField = new JTextArea("TEST TEXT!!!");
    statusField.setEditable(false);
    headerPane.add(statusField);
    startButton = new JButton("Start");
    closeButton = new JButton("Close");

    startButton.addActionListener(this);
    closeButton.addActionListener(this);

    this.setLayout(new BorderLayout());
    this.setSize(500, 400);

    headerPane.setPreferredSize(new Dimension(500, 50));

    mainPane = new JPanel();
    mainPane.setLayout(new BorderLayout());
    mainPane.add(headerPane, BorderLayout.NORTH);

    this.setVisible(true);
}

正如您在上面看到的,测试的目标只是生成 GUI 组件。

我想知道的是,我的测试只覆盖了第一行,super("Title");

因此,我的代码覆盖率大大降低了。

我可以在声纳报告中看到这个结果。

为什么我的测试没有覆盖左边的代码?

【问题讨论】:

    标签: java unit-testing junit gui-testing


    【解决方案1】:

    有一个异常,在eclipse中没有发现,但是在jenkins中发现了。

    为了解决这个问题,jenkins 中需要 xvnc 插件,并且服务器应该安装 vncserver。

    实际上,异常是“headlessException”,它只在 jenkins 中发现,而不是在 eclipse 中发现,尽管您的测试成功。

    所以,检查unit test success > yourTest.java > show details。如果您遇到我遇到的错误,您可以在此处查看错误消息。

    参考:Java HeadlessException? 这是我的问题和答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多