Festive Functional Tests for Swing GUIs
Very recently, Alex Ruiz announced the 1.0 Alpha release of FEST-Swing. An opportune moment to explore what it gives you. As opposed to unit tests, which focus on particular methods in an application, functional tests evaluate user scenarios. For example, instead of testing whether method "getUsers" works, a functional test walks through a small yet comprehensive scenario that a user of the application would be expected to complete. For example, in the case of an anagram game, a functional test would enter some text into a text field, click a button, and then compare the returned message with the message that the test writer would expect to be returned. Something like this, in semi-pseudo code:
window.textBox("anagramField").enterText("abstraction");
window.button("guessButton").click();
window.label("resultLabel").requireText("Correct! Try a new word!");
The cool thing is that the above isn't pseudo code at all. It is FEST-Swing (Fixtures for Easy Software Testing). Here is the whole test class in its entirety:
import com.toy.anagrams.ui.Anagrams;
import org.junit.Test;
import org.fest.swing.fixture.FrameFixture;
import org.junit.After;
import org.junit.Before;
public class AnagramFestTest {
private FrameFixture window;
@Before
public void setUp() {
window = new FrameFixture(new Anagrams());
window.show();
}
@Test
public void shouldEnterAnagramAndReturnTrue() {
window.textBox("anagramField").enterText("abstraction");
window.button("guessButton").click();
window.label("resultLabel").requireText("Correct! Try a new word!");
}
@After
public void tearDown() {
window.cleanUp();
}
}
That's not bad for a test. When you run the above, the anagram application starts up and then you can see with your own two eyes some text being entered in the text field, the button being clicked, and the result being returned, after which a success/failure output is given, with some helpful info for debugging. And, as you can see, you can use it together with JUnit (or TestNG, if that's what you prefer). The arguments to the "textBox", "button", and "label" invocations are the names of the Swing components in question. So, in the case of the JTextField below, one can see that the name property is set to "anagramField" below, which is sufficient for the FEST infrastructure to find it in the test above:
Especially when you compare it to, for example, the NetBeans XTest framework, the succinctness of FEST is even clearer. With the XTest framework, the meat of the above test would be something like this:
public void shouldEnterAnagramAndReturnTrue() {
JFrameOperator frmAnagrams = new JFrameOperator("Anagrams");
JLabelOperator lblGuess = new JLabelOperator(frmAnagrams, "Your Guess:");
JTextFieldOperator txtGuess = new JTextFieldOperator((JTextField)lblGuess.getLabelFor());
txtGuess.typeText("abstraction");
new JButtonOperator(frmAnagrams, "Guess").push();
JLabelOperator lblFeedback = new JLabelOperator(frmAnagrams, 2);
String expectedMessage = "Correct! Try a new word!";
assertEquals("Evaluation was wrong:", expectedMessage, lblFeedback.getText());
} That code is quite a bit denser and more verbose. FEST benefits from its "fluent" approach, which points (as stated in fluent's co-originator Martin Fowler's blog) to its attempt "to be readable and to flow". You can literally knock a quick FEST test together without too much thinking, so long as you know the scenario you want to deal with and the names of the Swing components that are involved. Not much documentation is needed either if you play with your IDE's autocomplete functionality.
What's also pretty cool is that FEST's Alex Ruiz will be presenting at JavaOne this year, as this blog entry of his explains, together with an interesting outline of what one can expect.
Here are some resources, for if you're new to FEST and want to take it for a spin:
- FEST-Swing Home Page
- Announcement of latest release
- Getting Started with FEST Swing
- Test -driven GUI development with FEST
- Alex Ruiz's Weblog
| Attachment | Size |
|---|---|
| fest-test.png | 107.29 KB |





Comments
Alex Ruiz replied on Sat, 2008/04/19 - 12:03pm
Hi Geertjan,
Thank you so much for helping us promote FEST! ;)
Very nice article! Thank for your kind words!
Yvonne (FEST co-author) and I are going to be speaking at JavaOne (Session TS-5212 - GUI Testing Made Easy.) We would love to catch up with you :)
Once again, many thanks!
Best regards,
-Alex.
Geertjan Wielenga replied on Sun, 2008/04/20 - 11:58am
Hi Alex, I will come to your session and I look forward to meeting you. I've also made a Fest plugin for NetBeans IDE:
http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=7964
Hope to meet up with you,
Geertjan
Alex Ruiz replied on Sun, 2008/04/20 - 3:20pm
That is awesome!!
Thanks a lot Geertjan!! I already updated the project's documentation with links to this article and the plugin! :)
See you soon!
-Alex
Konstantin Chikarev replied on Sat, 2008/05/17 - 7:55am
Geertjan Wielenga replied on Sat, 2008/05/17 - 8:07am
Personally, I believe the Jemmy/Jelly support for NetBeans RCP applications is better to use, because there are mock objects for the typical NetBeans RCP objects, such as TopComponents. I.e., the NetBeans team has really tailored this support to their specific needs, while Fest is more generic. Have a look at this, for an example:
http://blogs.sun.com/geertjan/entry/gui_testing_on_the_netbeans1
be guiz replied on Wed, 2009/09/02 - 8:57pm
Pippifin Cakasey replied on Sat, 2011/10/22 - 8:36am
James Walker replied on Wed, 2012/06/20 - 7:28am
Passion Lab replied on Sun, 2012/09/02 - 3:44pm
Lucifer Williams replied on Fri, 2012/11/16 - 1:32pm