开发者

Testing program java

开发者 https://www.devze.com 2023-04-11 16:08 出处:网络
I have written a program, and now I am required to write a \"runnable test script\" to test it. I suppose a runnable test script means a main method.

I have written a program, and now I am required to write a "runnable test script" to test it.

I suppose a runnable test script means a main method.

But all the methods call of the program are trigger when mouse clicked a button, or input from OptionPane. So, How can I write a runnable test script to control mouse click?

I don't know if I have describe the problem clearly. The program is a project task management tool, so you can clicked button to create a new project, save project, add tasks, add staff members, and so on. The problem is how do I control which button is to click by java code?

Here is a method that is called when a button is clicked:

public void newProject() {
    if (currentProject == null || showConfirm(
            "The current progress will not be saved.\nContinue?")) {

        resetCurrentProject();
        //Get the name of the new project
        String name = JOptionPane.showInputDialog(this, 
                "Please give a name to the project", "New Project", 
                JOptionPane.QUESTION_MESSAGE);

        if (name != null) {

          //Initialize the a new project
           开发者_JAVA技巧 ArrayList<Task> taskList = new ArrayList<Task>();
            ArrayList<Staff> staffList = new ArrayList<Staff>();

            //Create a new Project instance
            try {
                currentProject = new Project(name, staffList, taskList);
            } catch (Exception e1) {
                showError(e1.getMessage());
            }

            //Update the project information panel        
            updateProjectInfoPane();
        }
    }
}


Simple explanation: Instead of actually controlling the mouse, you get a reference to the button you want to emulate a click on. After you have a reference to the JButton (assuming Swing), you can call doClick() on it (http://download.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html#doClick%28%29).

For JOptionPane I have no idea, since I've never used it and don't know what it does, but the documentation can be found here regardless: http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html


If I were writing this, I would separate the interface from the logic. This way, my buttons are just calling underlying classes, my displays are calling underlying classes, and I can make my test script just call the underlying classes too!

Consider the following as if it wasn't swing but some other type of interface. The example I gave is pretty lame, because all it does is increment. But the idea is that any logic Incrementer has is now encapsulated in the class. This means it can be easily tested, extended, and modified if necessary.

// bad
class GUI {
    Button b = new ...
    int i = 0;
    TextArea t = new ...

    // bad code
    actionPerformed(Button b) {
        if(b == this.b) i++; // on button press increment
    }
    // whatever, you get the idea
    paint(Graphics g) {
        t.setText(i + "");
    }

}

// good code
class GUI {
    Button b = new ...
    int i = 0;
    TextArea t = new ...

    // bad code
    actionPerformed(Button b) {
        if(b == this.b) i.increment(); // on button press increment
    }
    // whatever, you get the idea
    paint(Graphics g) {
        t.setText(i.toString());
    }

}

class Incrementer {
    int value;
    void increment() { value++; }
    public String toString() { return Integer.valueOf(value); }
}


Clicking buttons (i.e. GUI) is not the sort of thing you should test (extensively). Leave it to the testers - they either test it manually or automate (Selenium, QTP).

I'm guessing you're required to write some unit tests...? In this case just write a test for each of your methods and job done. You're not supposed to test the front-end, but just the logic. For example, test your newProject() method by writing a unit test that checks whether the new project was initialised correctly. So, from your test method you call newProject() and assert if a new instance of the Project was created or not.

Some decent tutorial: http://www.michaelminella.com/testing/unit-testing-with-junit-and-easymock.html

GUI testing - do simple checks yourself (manual clicking) and pass it on to testers. If you don't work with any testers then I'm afraid the way to go is: 1. keep testing manually after any significant change, 2. Consider using Selenium http://seleniumhq.org/ or any other automation tool.

HTH, Damo

0

精彩评论

暂无评论...
验证码 换一张
取 消