Automated Swing Testing

For testing Swing UI, Jemmy is a really great tool. Some info about this can be found at

http://wiki.netbeans.org/Jemmy

and

http://jemmy.java.net/

and this is really useful powerpoint presentation I googled across:
Here

Another great tool is Fest-Swing , this uses Robot to generate Input Simulation.

I prefer Jemmy as it provides the option of using either Event Dispatching (events are programmatically created and dispatched onto the Event Queue) or Robot (using the Robot class to control the mouse and keyboard, generating native OS events like a real user using the UI) method for input simulation.

When you run the Tests on the Continuous build system, using Event Dispatch (the default mechanism for Jemmy) , it means you don’t need to have the process running inside a logged in windows desktop (it will still work even when it’s run as windows service account). The other advantage of Event Disptaching mechanism is that you when run the automated tests on your own machine you do not have to worry about your mouse and keyboard being taken control by the automated tests.

Experimenting with Java lambda syntax

On the lambda dev forum it was announced the Java lambda syntax ( planned for Java 8 ) will be based on the C# syntax., it will use thin arrow “->” rather C#’s fat arrow “=>” . I was curious to see what Java code with lambdas will look like using Swing.

To experiment with lambda, you can use a special build of the JDK with lambda support

http://jdk8.java.net/lambda/

To compile java files using the lambda features, nake sure you using javac is taken from the bin directory of this lambda jdk build.

Below is the non lambda version of the Example – using anonymous inner classes for the ActionListener and for the Runnable.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class TestInnerClass{

public static void showUI()
{
JFrame frame = new JFrame("Anonymous Inner Class Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me");

button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e) {
System.out.println("button clicked");
}
}
);

Container contentPane = frame.getContentPane();
contentPane.add(button, BorderLayout.CENTER);
frame.setSize(300, 100);
frame.setVisible(true);
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showUI();
}
});

}
}

 

The above is not very interesting, now let’s convert those inner classes to use the lambda syntax..

 

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class TestLambda{

public static void showUI()
{
JFrame frame = new JFrame("Lambda Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Click Me");

button.addActionListener( (e) -> {System.out.println("button clicked");});

Container contentPane = frame.getContentPane();
contentPane.add(button, BorderLayout.CENTER);
frame.setSize(300, 100);
frame.setVisible(true);
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> {showUI();});

}
}

 
Using the lambda syntax, the code looks more concise, it has less ceremony (code that offers very little information).