FEST JUnit-Swing测试noobQ:如何测试主类?

时间:2011-03-03 14:56:43

标签: swing gui-testing fest

尽管阅读了教程here,我似乎无法理解如何使FEST与我的应用程序一起工作。

我在一个带有main方法的大类中有一个Swing应用程序,还有一些SwingWorker类。我想测试我的应用程序,就像我通过main方法运行它一样。本教程似乎只给出了如何测试单个组件的说明。

我的ApplicationWindow.class的微缩版本,其中包含主要方法:

private JFrame frmArtisol;
private JButton btnBrowseDB;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ApplicationWindow window = new ApplicationWindow();
                window.frmArtisol.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ApplicationWindow() {
    initialize();

}

我的测试类抛出错误。

public class ApplicationWindowTest {
private FrameFixture window;
@Before
public void setup() throws InitializationError{
    ApplicationWindow applicationWindow = new ApplicationWindow();
    JFrame frame = applicationWindow.getFrmArtisol(); 
    frame = GuiActionRunner.execute(new GuiQuery<JFrame>() {

        @Override
        protected JFrame executeInEDT() throws Throwable {
            return new JFrame();
        }
    });

    window = new FrameFixture(frame);
    window.show();

}

@Test
public void test(){
    window.button("btnBrowseDB").click();
}
@After
public void after(){
    window.cleanUp();
}

}

运行此测试时抛出的错误:

  org.fest.swing.exception.ComponentLookupException: Unable to find component using matcher org.fest.swing.core.NameMatcher[name='btnBrowseDB', type=javax.swing.JButton, requireShowing=true].

Component hierarchy:
javax.swing.JFrame[name='frame0', title='', enabled=true, visible=true, showing=true]
  javax.swing.JRootPane[]
    javax.swing.JPanel[name='null.glassPane']
    javax.swing.JLayeredPane[]
      javax.swing.JPanel[name='null.contentPane']

    at org.fest.swing.core.BasicComponentFinder.componentNotFound(BasicComponentFinder.java:271)
    at org.fest.swing.core.BasicComponentFinder.find(BasicComponentFinder.java:260)
    at org.fest.swing.core.BasicComponentFinder.find(BasicComponentFinder.java:254)
    at org.fest.swing.core.BasicComponentFinder.findByName(BasicComponentFinder.java:191)
    at org.fest.swing.fixture.ContainerFixture.findByName(ContainerFixture.java:527)
    at org.fest.swing.fixture.ContainerFixture.button(ContainerFixture.java:124)
    at ApplicationWindowTest.test(ApplicationWindowTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

似乎跑步者没有找到我的组件,这让我相信我误解了如何测试这种事情。非常感谢任何指导我正确方向的帮助。

1 个答案:

答案 0 :(得分:2)

您可能已经解决了这个问题,但我在寻找相关问题的帮助时遇到了您的问题。

问题是,您的setup()方法会创建ApplicationWindow,但会覆盖变量frame,并引用一个全新且无关的JFrame对象。您要做的是在ApplicationWindow方法中创建executeInEDT(),如下所示:

@Before
public void setup() throws InitializationError{
    JFrame frame = GuiActionRunner.execute(new GuiQuery<JFrame>() {

        @Override
        protected JFrame executeInEDT() throws Throwable {
            return new ApplicationWindow().getFrmArtisol();
        }
    });

    window = new FrameFixture(frame);
    window.show();

}