简体   繁体   中英

How do I test JButton ActionListener from within a method? (unit testing)

I am creating a board game in Eclipse and trying to do unit testing. For my unit testing, I am using Junit and EclEmma. Within a class, I am trying to test that if a button (newGameButton) is pressed, it will take the player to the contentPane "Player Details".

Here is the relevant portion of the code for the JButton:

public void createGUI() {
    JButton newGameButton = new JButton("New Game");
    newGameButton.setToolTipText("Click to start a new game");
    newGameButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            CardLayout cardLayout = (CardLayout) contentPane.getLayout();
            cardLayout.show(contentPane, "Player Details");
        pack();
    }
});

And my test code (in a separate class) is:

@Test
public void testHomeButtonMainMenu()
{
    appTest.createGUI();
    appTest.newGameButton.doClick(); // I know this isn't correct, I'm trying to simulate newGameButton being pressed
    Assert.assertTrue(); // somehow test that the game is on the player details
}

Does anyone know how to correct code the JButton newGameButton being pressed and what test to write to show that the test was successful?

Thanks

I would split up your testing into two completely separate parts. Unit test the code under the GUI and use something like WindowLicker for driving the actual UI.

The book Growing Object Oriented Software: Guided By Tests walks you through this as well as how to split up your tests to be more manageable and give you a much more well designed and flexible system.

You are using an anonymous inner class. There is nothing wrong with that, even though it is ugly. What is wrong, as far as TDD is concerned, however, is that it contains more than a call to a single method of the enclosing class . If you write it that way, you can easily unit test that method without having to press any buttons ;)

The test would assert state changes of the method's enclosing class expected to happen when the method executes. Since this method makes sense to be void, you can't assert anything else on a unit test, really.

To take it one step further, the command that is executed on a Button need not be nowhere near the UI component that has that Button (many buttons, in reality affect mostly other components). You just need to create an abstraction to help you pass around command objects. If you are interested, you can have a look at this post http://bo2framework.blogspot.gr/2013/08/bo2-concepts-part-1-service-panels.html

As far as testing the GUI is concerned, you would most likely need a tool for black box testing. Unfortunately, I am not familiar with Swing application black box testing, but 'swing black box testing' would be a good first Google search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM