Launching Applications from a "main" Method


FEST-Swing supports launching an application from its "main" method. It is quite easy, and requires one line of code. We only need to use ApplicationLauncher.

Let's assume we have the following application:

  public class JavaApp {

    public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          JFrame frame = new JFrame("Java Application");
          frame.setPreferredSize(new Dimension(200, 200));
          frame.pack();
          frame.setVisible(true);
        }
      });
    }
  }

We can launch the application programmatically in different ways:

  //import static org.fest.swing.launcher.ApplicationLauncher.*;

  application(JavaApp.class).start();

  // or

  application("com.mycompany.JavaApp").start();

The following example shows how to start an application with arguments:

  application(JavaApp.class).withArgs("arg1", "arg2").start();

  // or

  application("com.mycompany.JavaApp").withArgs("arg1", "arg2").start();

Once the application is started, we can get a reference its JFrame. In this example we are using a WindowFinder:

  // import static org.fest.swing.finder.WindowFinder.findFrame;

  FrameFixture frame = findFrame(new GenericTypeMatcher<Frame>() {
    protected boolean isMatching(Frame frame) {
      return "Java Application".equals(frame.getTitle()) && frame.isShowing();
    }
  }).using(robot);

For more information about WindowFinder, please visit Testing Long-Duration Tasks.




Javadocs

Reports

edit SideBar