2011-03-03 63 views
2

儘管閱讀了教程here,我似乎無法理解如何使FEST與我的應用程序一起工作。FEST JUnit-Swing測試noobQ:如何測試主類?

我有一個SwingWorker類的主要方法和一個大類中的Swing應用程序。我想測試我的應用程序,就好像通過主要方法運行它一樣。本教程似乎只給出瞭如何測試單個組件的說明。

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

} 

而且我的TestClass拋出一個錯誤。

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) 

看來,如果跑步者沒有找到我的組件,這使我相信,我有誤解如何測試這種事情。任何幫助指引我在正確的方向非常感謝。

回答

2

您可能已經解決了這個問題,但在尋找相關問題的幫助時,我遇到了您的問題。

問題是您的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(); 

}