FEST JUnit-Swing testing noobQ: how to test a main class?

2.7k views Asked by At

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.

1

There are 1 answers

0
Ben On

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

}