main() methodAssertJ Swing supports launching an application from its main() method. It is quite
easy, and requires only one line of code. You only need to use the
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 this application programmatically either by the main class or its fully qualified name:
//import static org.assertj.swing.launcher.ApplicationLauncher.*;
// 1. by class object without arguments
application(JavaApp.class).start();
// 2. by class object with arguments
application(JavaApp.class).withArgs("arg1", "arg2").start();
// 3. by qualified name without arguments
application("com.mycompany.JavaApp").start();
// 4. by qualified name with arguments
application("com.mycompany.JavaApp").withArgs("arg1", "arg2").start();
Once the application is started, we can get a reference of its JFrame. In this example we're
using a WindowFinder. For more information about WindowFinder, please visit
Testing Long-Duration Tasks.
// import static org.assertj.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);
AssertJ Swing provides its own applet viewer to support GUI testing of Java applets. Testing an applet involves three steps: start the applet, the actual test and resource cleanup.
There are two ways to start an applet:
AppletLauncher to start an AppletViewerThe class AppletLauncher provides a fluent interface for launching an applet:
AppletViewer viewer = AppletLauncher.applet("org.assertj.swing.applet.MyApplet").start();
// or
AppletViewer viewer = AppletLauncher.applet(MyApplet.class).start();
// or
AppletViewer viewer = AppletLauncher.applet(new MyApplet()).start();
In addition, we can pass parameters to the applet. This works as if you'd specify them in the
param tag of the embedding HTML:
AppletViewer viewer = AppletLauncher.applet(new MyApplet())
.withParameters(
name("bgcolor").value("blue"),
name("color").value("red"),
name("pause").value("200")
)
.start();
// or
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("bgcolor", "blue");
parameters.put("color", "red");
parameters.put("pause", "200");
AppletViewer viewer = AppletLauncher.applet(new MyApplet()).withParameters(parameters).start();
AppletViewer directlyThe AppletLauncher provides a simple and compact API to start applets in an
AppletViewer. The created AppletViewer uses basic implementations of
AppletStub and AppletContext though. If you need to test an applet with more
advanced implementations of AppletStub or AppletContext, you can use
AppletViewer directly, passing your own AppletStub (which will return your own
AppletContext).
AppletViewer viewer = AppletViewer.newViewer(new MyApplet(), new MyAppletStub());
Once we have an AppletViewer up and running the applet to test, we simply can wrap
the AppletViewer with a FrameFixture and start testing.
private AppletViewer viewer;
private FrameFixture applet;
@BeforeMethod public void setUp() {
viewer = // get the viewer using AppletLauncher or create a new AppletViewer
applet = new FrameFixture(viewer);
applet.show();
}
@Test public void shouldChangeLabelOnButtonClick() {
applet.button("ok").click();
applet.label("text").requireText("Hello");
}
@AfterMethod public void tearDown() {
viewer.unloadApplet();
applet.cleanUp();
}
As our previous example showed, in addition to call cleanUp() we need to unload the applet by
calling unloadApplet in the AppletViewer used in our test.