lundi 1 juin 2015

Why my unit test doesn't cover gui component generation code?

I'm trying to do unit test for the code which generates GUI components.

This is my test code.

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

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

    assertTrue(true);
}

And the target code is here.

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);
}

As you can see above, the target of the test just generates the GUI components.

What I wonder is, my test just covers the first line, super("Title");.

For this reason, my code coverage reduced much.

And I could see this result in sonar report.

Why my test does not cover the left codes?

Aucun commentaire:

Enregistrer un commentaire