Java 单元测试将ActionEvent作为参数的函数

Java 单元测试将ActionEvent作为参数的函数,java,swing,unit-testing,Java,Swing,Unit Testing,如何在Java/Swing中测试采用“ActionEvent”类型参数的函数?以下是我要测试的函数: public void actionPerformed(ActionEvent e) { // Communicating with the Model class // /** if the "Add" button has been pressed **/ if (e.getActionCommand(

如何在Java/Swing中测试采用“ActionEvent”类型参数的函数?以下是我要测试的函数:

public void actionPerformed(ActionEvent e)
    {
                            // Communicating with the Model class //

        /** if the "Add" button has been pressed **/
    if (e.getActionCommand() == "Add")
    {
        String cust_name = view.getAddCustNameField().getText();
        int ph_num = Integer.parseInt(view.getAddPhoneNumField().getText());
        model.addEntry(cust_name, ph_num, "controller");
    }

    /** if the "Delete" button has been pressed **/
    else if (e.getActionCommand() == "Delete")
    {
        String cust_name = view.getDelCustNameField().getText();
        model.deleteEntry(cust_name, "controller");
    }

    /** if the "Update" button has been pressed **/
    else if (e.getActionCommand() == "Update")
    {
        String cust_name = view.getUpdCustNameField().getText();
        int ph_num = Integer.parseInt(view.getUpdPhoneNumField().getText());
        model.updateEntry(cust_name, ph_num, "controller");
    }

                        // Communicating with the View class //     
    else 
    {

        /** if the "Change Family" button has been pressed **/
        Font fontRes = view.getResDisplayField().getFont();
        Font fontDir = view.getDirDisplayField().getFont();
        if (e.getActionCommand() == "Change Family")
        {
            String fontFamily = view.getFontFamilyField().getText();    
            if (fontFamily != "")
            {
                view.getResDisplayField().setFont(new Font(fontFamily, fontDir.getStyle(), fontRes.getSize()));
            }
        }

        /** if the "Change Size" button has been pressed **/
        else if (e.getActionCommand() == "Change Size")
        {
            int fontWeight = Integer.parseInt(view.getFontSizeField().getText());               
            if (fontWeight > 0)
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), fontRes.getStyle(), fontWeight));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), fontDir.getStyle(), fontWeight));
            }
        }

        /** if the "Change Italics" button has been pressed **/
        else if (e.getActionCommand() == "Change Style")
        {
            String fontStyle = (String)(view.getFontStyleCombo().getSelectedItem());
            if (fontStyle == "Plain")
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), Font.PLAIN, fontRes.getSize()));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), Font.PLAIN, fontDir.getSize()));
            }

            else if (fontStyle == "Italic")
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), Font.ITALIC, fontRes.getSize()));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), Font.ITALIC, fontDir.getSize()));
            }

            else if (fontStyle == "Bold")
            {
                view.getResDisplayField().setFont(new Font(fontRes.getFamily(), Font.BOLD, fontRes.getSize()));
                view.getDirDisplayField().setFont(new Font(fontDir.getFamily(), Font.BOLD, fontDir.getSize()));
            }
        }

    }// 'else' (if the command is meant for the view class)

所以,基本上我想模拟按钮按下动作,并根据按下的特定按钮执行条件语句的一个分支与每个按钮对应的是一组2个JTextFields,我必须在单元测试函数中设置它们的值。这些是“…custnamfield”和“…PhoneNumField”,我在上面的函数中输入了它们的值,因此我必须事先设置它们的值,以便我可以直接输入它们。我怎样才能做到

您可以使用类似easymock或mokito的模拟框架

这些框架允许您:

  • 为接口创建模拟对象
  • 动态记录预期的方法调用
  • 将模拟对象置于播放模式,然后
  • 将其传递给要测试的方法
  • 验证呼叫是否按预期进行

不清楚您到底想做什么。“我想模拟按键动作”和“我必须事先设置它们的值,以便我可以直接输入它们。我如何才能这样做?”是两件不同的事情。你能根据你所拥有的和你想做的重新构造你的问题吗?