【发布时间】: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