【问题标题】:FEST JUnit-Swing testing noobQ: how to test a main class?FEST JUnit-Swing 测试 noobQ:如何测试一个主类?
【发布时间】:2011-03-03 14:56:42
【问题描述】:

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

我在一个大类中有一个 Swing 应用程序,它有一个 main 方法和几个 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)

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

【问题讨论】:

    标签: swing gui-testing fest


    【解决方案1】:

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

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

    @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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      相关资源
      最近更新 更多