FEST:无法使用matcher org.fest.swing.core.NameMatcher查找组件

时间:2014-12-27 11:19:31

标签: java unit-testing fest

我尝试使用FEST进行简单测试但失败了。这是我的Swing应用程序:

public final class App extends JFrame {
  public App() {
    super();
    JButton button = new JButton("start!");
    button.setName("start");
    this.getContentPane().add(button);
  }
}

这是测试(我使用JUnit 4):

public final class AppTest {
  @Test
  public void test() {
    FrameFixture frame = new FrameFixture(new App());
    frame.button("start").click();
    frame.cleanUp();
  }
}

这是失败的原因:

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

Component hierarchy:
com.sigzig.App[name='frame0', title='', enabled=true, visible=false, showing=false]
  javax.swing.JRootPane[]
    javax.swing.JPanel[name='null.glassPane']
    javax.swing.JLayeredPane[]
      javax.swing.JPanel[name='null.contentPane']
        javax.swing.JButton[name='start', text='start!', enabled=true, visible=true, showing=false]

  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)
    ...

这是我使用的依赖:

<dependency>
    <groupId>org.easytesting</groupId>
    <artifactId>fest-swing</artifactId>
    <version>1.2.1</version>
    <scope>test</scope>
</dependency>

出了什么问题?

1 个答案:

答案 0 :(得分:2)

只需将其添加到您的应用构建器:

this.setVisible(true);