2015-06-01 32 views
0

我正在嘗試爲生成GUI組件的代碼進行單元測試。爲什麼我的單元測試不包括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");

由於這個原因,我的代碼覆蓋率大大降低了。

我可以在聲納報告中看到這個結果。

爲什麼我的測試不包括左代碼?

回答

0

有一個例外,這在日食中找不到,但在jenkins中找到。

爲了解決這個問題,在jenkins中需要xvnc插件,vncserver應該安裝在服務器上。

實際上,這個例外是'headlessException',它只能在jenkins中找到,而不是eclipse,儘管你的測試成功了。

所以,檢查unit test success > yourTest.java > show details。如果您遇到錯誤,您可以在這裏看到錯誤消息。

參考:Java HeadlessException? 這是我的問題和答案。

相關問題