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

发布于 2024-10-20 10:01:33 字数 4599 浏览 0 评论 0原文

尽管阅读了此处的教程,但我似乎无法理解如何使 FEST 与我的申请。

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

我的 ApplicationWindow.class 的微型版本,包含 main 方法:

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)

运行程序没有找到我的组件,这使我相信我误解了如何测试此类事物。非常感谢任何为我指明正确方向的帮助。

Despite reading the tutorial here, I cant seem to understand how to make FEST work with my application.

I have a Swing application in a big class witht a main method, and a couple of SwingWorker classes. I want to test my application as if I'm running it via the main method. The tutorial only seemed to give instructions on how to test a single component.

Miniature version of my ApplicationWindow.class that contains the main method:

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

}

And my testclass throwing an error.

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

}

The error thrown when running this test:

  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)

It seems as if the runner doesn't find my component, and this leads me to believe that I have misunderstood how to test this kind of thing. Any help pointing me in the right direction is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-10-27 10:01:33

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

问题是您的 setup() 方法创建了一个 ApplicationWindow,但随后使用对全新且不相关的 的引用覆盖了变量 frame >JFrame 对象。您想要做的是在 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();

}

You may have already solved this, but I just came across your question while looking for help with a related problem.

The problem is that your setup() method creates an ApplicationWindow but then overwrites the variable frame with a reference to a completely new and unrelated JFrame object. What you want to do is create the ApplicationWindow in the executeInEDT() method like this:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文