Search code examples
javaswingjunitjbuttonactionlistener

How to jUnit test an ActionListener


My class has the actionPerformed method that I want to run a JUnit test on:

@Override
public void actionPerformed(ActionEvent actionEvent) {
    try {
        if (actionEvent.getSource() == returnButton && previousWindow.equals("menu")) {
            MenuWindow menuWindow = new MenuWindow();
            menuWindow.setMenuWindow();
        }
        if (actionEvent.getSource() == returnButton && previousWindow.equals("download")) {
            DownloadWindow downloadWindow = new DownloadWindow();
            downloadWindow.setDownloadWindow();
        }
        if (actionEvent.getSource() == returnButton && previousWindow.equals("upload")) {
            UploadWindow uploadWindow = new UploadWindow();
            uploadWindow.setUploadWindow();
        }
    } catch (Exception e) {
        LOGGER.info(e.toString());
    }
}

I have heard something about a doClick method for calling the actionListener but the JButton is a local variable so I'm not sure how to call it:

/**
 * This method sets the variables of the frame to be put in the help window
 * @return
 */
public JFrame setHelpWindow() {
    JFrame helpFrame = new JFrame();
    helpFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    helpFrame.setLayout(new GridBagLayout());
    helpFrame.setTitle("Help");
    helpFrame.setSize(700, 300);
    helpFrame.setLocationRelativeTo(null);
    helpFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    helpPanel = setHelpPanel();
    helpFrame.add(helpPanel, frameGbc);
    helpFrame.getContentPane().setLayout(new GridBagLayout());
    helpFrame.setVisible(true);
    return helpFrame;
}

/**
 * This method sets the variables of the panel to be put in the frame
 * @return
 */
private JPanel setHelpPanel() {
    panelGbc.fill = GridBagConstraints.HORIZONTAL;
    panelGbc.insets.bottom = 1;
    panelGbc.insets.top = 1;
    panelGbc.insets.right = 1;
    panelGbc.insets.left = 1;
    panelGbc.weightx = 1;
    panelGbc.weighty = 1;
    helpPanel.setLayout(new GridBagLayout());
    setText(helpPanel);
    setButtons(helpPanel);
    setAction();
    helpPanel.setSize(700, 300);

    return helpPanel;
}

/**
 * This method sets the variables of the buttons
 * @param helpPanel
 */
private void setButtons(JPanel helpPanel) {
    returnButton = new JButton("Return");
    panelGbc.gridx = 0;
    panelGbc.gridy = 2;
    panelGbc.gridwidth = 1;
    panelGbc.gridheight = 1;
    helpPanel.add(returnButton, panelGbc);
}

If anybody could tell me how to test the JButton and get coverage of the actionListener I'd be very grateful. Thank you.


Solution

  • In the end I used a doClick and tested that an exception wasn't thrown:

    @Test
    @DisplayName("ActionListener test")
    void testActionListener(){
        HelpWindow helpWindow = new HelpWindow("menu");
        helpWindow.setHelpWindow();
    
        assertDoesNotThrow(() -> helpWindow.getReturnButton().doClick());
    }
    

    I created a getter for the JButton, activated with the doClick and tested with the assertDoesNotThrow. Gave me 100% coverage and tested all the lines (to do with that button press).