如何 jUnit 测试 ActionListener
How to jUnit test an ActionListener
我的 class 具有我想要 运行 JUnit 测试的 actionPerformed 方法:
@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());
}
}
我听说过有关调用 actionListener 的 doClick 方法,但 JButton 是一个局部变量,所以我不确定如何调用它:
/**
* 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);
}
如果有人能告诉我如何测试 JButton 并了解 actionListener,我将不胜感激。谢谢。
最后我用了一个doClick并测试没有抛出异常:
@Test
@DisplayName("ActionListener test")
void testActionListener(){
HelpWindow helpWindow = new HelpWindow("menu");
helpWindow.setHelpWindow();
assertDoesNotThrow(() -> helpWindow.getReturnButton().doClick());
}
我为 JButton 创建了一个 getter,用 doClick 激活并用 assertDoesNotThrow 测试。给我 100% 的覆盖率并测试了所有线路(与按下按钮有关)。
我的 class 具有我想要 运行 JUnit 测试的 actionPerformed 方法:
@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());
}
}
我听说过有关调用 actionListener 的 doClick 方法,但 JButton 是一个局部变量,所以我不确定如何调用它:
/**
* 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);
}
如果有人能告诉我如何测试 JButton 并了解 actionListener,我将不胜感激。谢谢。
最后我用了一个doClick并测试没有抛出异常:
@Test
@DisplayName("ActionListener test")
void testActionListener(){
HelpWindow helpWindow = new HelpWindow("menu");
helpWindow.setHelpWindow();
assertDoesNotThrow(() -> helpWindow.getReturnButton().doClick());
}
我为 JButton 创建了一个 getter,用 doClick 激活并用 assertDoesNotThrow 测试。给我 100% 的覆盖率并测试了所有线路(与按下按钮有关)。