简体   繁体   English

如何从方法中测试JButton ActionListener? (单元测试)

[英]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. 我正在Eclipse中创建一个棋盘游戏,并尝试进行单元测试。 For my unit testing, I am using Junit and EclEmma. 对于单元测试,我正在使用Junit和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". 在一个类中,我试图测试是否按下了一个按钮(newGameButton),它将把玩家带到contentPane“ Player Details”。

Here is the relevant portion of the code for the JButton: 这是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? 有谁知道如何更正按下JButton newGameButton的代码以及编写哪种测试来表明测试成功?

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. 在GUI 对代码进行单元测试,并使用WindowLicker之类的东西来驱动实际的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 . 然而,就TDD而言,出问题的是,它不仅包含对封闭类的单个方法的调用。 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). 更进一步,在Button上执行的命令不必在具有该Button的UI组件附近(实际上,许多按钮实际上会影响其他组件)。 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 如果您有兴趣,可以看看这篇文章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. 就测试GUI而言,您很可能需要用于黑盒测试的工具。 Unfortunately, I am not familiar with Swing application black box testing, but 'swing black box testing' would be a good first Google search. 不幸的是,我不熟悉Swing应用程序黑盒测试,但是“ Swing黑盒测试”将是第一个不错的Google搜索工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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