Software Development

A Guide to Effective Testing for Swing Applications

Hey there! Ever wonder how we make sure those cool and interactive Swing Applications work smoothly? Well, testing is the secret sauce, and in this article, we’re diving into the testing world of Swing apps using AssertJ and JUnit 5. We’ll guide you through with a real-life example – a GUI app for the Posmulten library.

We’ll break down how AssertJ and JUnit 5 team up to create a solid and easy-to-use testing framework for Swing applications. Plus, we’re throwing in GitHub Actions to automate testing, making sure our Swing app stays strong and bug-free as it grows.

Come along as we explore the ins and outs of testing Swing apps, make sense of how AssertJ and JUnit 5 work together, and use GitHub Actions to keep our Posmulten library GUI app super reliable. Let’s make sure our graphical interfaces can handle the complexity like pros!

1. Choosing the Right Testing Framework

Let’s explore an example using a library for testing Swing applications – FEST Swing. FEST Swing is a popular testing library that provides a concise and expressive API for interacting with Swing components.

Example Test Case with FEST Swing:

Assume we have a Swing application with a text field and a button, and we want to test that entering specific text and clicking the button results in the correct panel being displayed.

import org.fest.swing.edt.*;
import org.fest.swing.fixture.DialogFixture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.fest.swing.core.matcher.DialogMatcher.withTitle;
import static org.fest.swing.core.matcher.JButtonMatcher.withText;

public class MyAppUITest {

    private DialogFixture window;

    public void setUp() {
        // Set up and display the GUI
        executeInEDT(() -> {
            MyApp myApp = new MyApp();
            window = new DialogFixture(myApp);

    public void shouldDisplayCorrectPanelAfterButtonClick() {
        // Simulate user actions
        window.textBox("textInput").enterText("Hello, FEST Swing!");

        // Verify the expected panel is displayed
        DialogFixture resultDialog = window.dialog(withTitle("Result Dialog"));
        resultDialog.label("resultLabel").requireText("Result Panel");


  1. Setting Up the Test Environment:
    • The setUp method, annotated with @BeforeEach, creates and displays the GUI within the EDT.
  2. Simulating User Actions:
    • FEST Swing’s fluent API allows us to interact with Swing components concisely. Here, we use enterText to input text into a text field and click to simulate a button click.
  3. Verifying the Result:
    • FEST Swing provides matchers to identify Swing components. We use withText to identify the button and withTitle to identify the dialog. Then, we verify the content of the label within the dialog using requireText.

While the specific syntax differs between AssertJ Swing and FEST Swing, the underlying principles of GUI testing remain consistent. Both libraries empower developers to create readable and effective tests, ensuring the reliability of Swing applications. The choice between libraries often comes down to personal preference and the specific requirements of your testing environment.

2. Maven Configuration

It is advisable to segregate UI tests from unit tests, even if these tests are not entirely end-to-end and involve mocked components. This separation is recommended to prevent them from running concurrently with unit tests, as their execution time is typically longer than that of standard unit tests.

Code Snippet for Test Exclusion:

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

// Unit test class
public class MyUnitTest {

    public void myUnitTest() {
        // Unit test logic

// UI test class
public class MyUITest {

    public void myUITest() {
        // UI test logic

In the code snippet, tests are tagged using JUnit 5’s @Tag annotation. This allows you to categorize tests based on their type, such as “unit” or “ui.” During test execution, you can then selectively include or exclude tests based on their tags. This approach enables you to run unit tests and UI tests separately, preventing UI tests from affecting the speed of unit test execution.

3. Navigating UI Test Execution Challenges: A Headless Environment Approach

Executing UI tests in an environment with a Graphics card is straightforward, but challenges arise when running them in a headless environment, which is the default setting for many CI/CD pipelines. Issues may occur due to UI windows, notifications, or missing components. While Xvfb is a common suggestion, the AssertJ Swing contributors recommend an alternative – Tightvncserver. Let’s consider an advanced example that includes steps and configurations, such as handling dependencies, setting up a virtual framebuffer, and ensuring proper cleanup after the UI tests.

name: Advanced UI Tests

on: [push, pull_request]

    runs-on: ubuntu-latest

    - name: Set up Java and Dependencies
      uses: actions/setup-java@v2
        distribution: 'adopt'
        java-version: '11'

    - name: Install Dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y tightvncserver openbox libgtk-3-0 libxtst6

    - name: Start VNC Server with Openbox
      run: |
        tightvncserver :1 -geometry 1920x1080 -depth 24 -localhost no -nolisten tcp
        sleep 5  # Give VNC server time to start
        openbox &

    - name: Set Display Variable
      run: export DISPLAY=:1

    - name: Run UI Tests
      run: ./gradlew uiTest

    - name: Stop VNC Server
      run: vncserver -kill :1

    - name: Cleanup
      run: |
        sudo apt-get purge -y tightvncserver openbox
        sudo apt-get autoremove -y

In the provided GitHub Actions example, we start by setting up Java and installing necessary dependencies, including Tightvncserver, Openbox, and additional libraries. We then initiate a VNC server, configure its parameters, and start Openbox as the window manager. After setting the DISPLAY variable, we run the UI tests using Gradle. Finally, we stop the VNC server and perform cleanup, removing unnecessary dependencies to ensure a clean environment.

This example provides a more intricate setup for UI tests in a headless environment, taking into account additional configurations and offering a comprehensive approach to handle advanced scenarios.

4. Exploring AssertJ and JUnit 5 Synergy: A Symphony of Comprehensive and Expressive Testing

In Java testing, using AssertJ with JUnit 5 provides a strong and easy-to-use framework for checking code reliability. AssertJ is known for its clear and natural assertions, and when combined with the robust features of JUnit 5, it creates a partnership that goes beyond conventional testing approaches. This combination allows developers to write tests in a straightforward manner, ensuring the correctness of their code.

1. Fluent Assertions for Readable Tests: AssertJ’s strength lies in its fluent API, allowing developers to articulate assertions in a human-readable manner. When coupled with JUnit 5’s test annotations, the collaboration delivers tests that not only verify functionality but also narrate the expected behavior with clarity. This readability enhances collaboration among team members, making the testing process more accessible and comprehensible.

2. Comprehensive Collection of Assertions: AssertJ doesn’t merely stop at basic assertions; it extends its support to a comprehensive collection of specialized assertions. This extensive range empowers developers to validate intricate scenarios with ease. Combined with JUnit 5’s flexible test structure, it provides a versatile toolkit for handling diverse testing requirements, from simple unit tests to complex integration scenarios.

3. Seamless Integration with JUnit 5 Lifecycle: JUnit 5’s lifecycle and extension model seamlessly integrate with AssertJ, creating a testing environment that adapts to diverse project structures. The assertive capabilities of AssertJ complement JUnit 5’s annotations like @BeforeEach and @AfterEach, allowing developers to set up and tear down resources effortlessly. This integration ensures tests are executed predictably and consistently.

4. Enhanced Failure Reporting: When a test fails, understanding the root cause is crucial for swift debugging and issue resolution. AssertJ’s integration with JUnit 5 enhances failure reporting, providing detailed and informative messages about assertion failures. This aids developers in pinpointing issues efficiently, expediting the debugging process and reducing the time to resolution.

5. Parameterized Tests with AssertJ Goodness: JUnit 5’s support for parameterized tests, coupled with AssertJ’s expressive assertions, brings a new level of sophistication to test parameterization. The combination allows developers to write parameterized tests that not only cover a broad range of scenarios but also maintain the clarity and readability characteristic of AssertJ assertions.

In the world of testing, AssertJ and JUnit 5 play crucial roles, each contributing its own strengths. When used together, they form a testing framework that is not only powerful but also enjoyable for developers. AssertJ’s expressive features complement JUnit 5’s structured elegance, providing a testing experience that is both thorough and enjoyable. Exploring this collaboration reveals a realm of testing options, ensuring a testing process marked by clarity, efficiency, and effectiveness.

AssertJ and JUnit 5 work well together. Here’s a simple code example to show how they can be used for testing. In this example, we’ll create a JUnit 5 test class using AssertJ assertions to validate the behavior of a fictional Calculator class.

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;

public class CalculatorTest {

    void addTwoNumbers_shouldReturnSum() {
        // Arrange
        Calculator calculator = new Calculator();

        // Act
        int result = calculator.add(3, 4);

        // Assert

    void divideByZero_shouldThrowException() {
        // Arrange
        Calculator calculator = new Calculator();

        // Act and Assert
        assertThatThrownBy(() -> calculator.divide(10, 0))
                .hasMessage("Cannot divide by zero");

In this example:

  1. The addTwoNumbers_shouldReturnSum test demonstrates a simple addition scenario. The assertThat(result).isEqualTo(7) statement uses AssertJ to assert that the result of adding 3 and 4 is equal to 7.
  2. The divideByZero_shouldThrowException test showcases exception handling. The assertThatThrownBy method is an AssertJ feature that asserts that the specified operation (in this case, dividing by zero) results in a specific exception (ArithmeticException) with a particular error message.

This code snippet showcases the clarity and expressiveness of AssertJ assertions within the context of JUnit 5 tests, creating tests that are not only effective but also easy to read and understand.

5. Test Reliability and Stability

AspectImportanceWhy It Matters
Test Reliability and StabilityCriticalProvides accurate feedback about the application’s functionality. Easier maintenance of test suites. Efficient use of development and testing resources. Ensures the success of Continuous Integration and Deployment (CI/CD) processes. Instills confidence in builds and releases. Reduces technical debt by addressing issues promptly.
Containerization for UI TestingSignificantEnsures consistent and isolated environments for UI tests. Simplifies dependency management. Facilitates seamless integration into CI/CD pipelines. Enhances test portability and reproducibility.
Parallel Execution StrategiesImportantImproves testing efficiency by running tests concurrently. Reduces overall test execution time. Enhances scalability for larger test suites. Enables quicker feedback in the development process.
Integrating with Code Quality ToolsValuableEnsures not only functional correctness but also code maintainability. Enforces coding standards and best practices. Contributes to overall software quality and maintainability.
Handling Test Data and StateImportantManages test data and application state for reliable and repeatable tests. Ensures consistency and predictability in test outcomes. Supports test scenarios with specific data requirements.
Test Reporting and VisualizationSignificantEnhances visibility into test results. Facilitates analysis and troubleshooting of test failures. Contributes to better communication among team members. Supports data-driven decision-making.
Dynamic Test EnvironmentsImportant– Enables flexibility in configuring test environments based on testing requirements. Supports adaptability to different scenarios and conditions.
Handling Asynchronous OperationsImportantAddresses challenges related to asynchronous operations in UI tests. Ensures synchronization with dynamic UI changes. Enhances the reliability of UI tests in dynamic applications.
Continuous Monitoring for UI TestsValuableMonitors and manages UI tests across different environments. Provides insights into test performance and reliability. Contributes to the overall health of the testing infrastructure.
Integration with Test Case Management SystemsValuableStreamlines test case execution and result tracking. Enhances organization and traceability of test cases. Supports efficient test management practices.
Handling Flaky TestsImportantIdentifies and mitigates flaky tests to ensure test stability. Maintains trust in the testing process. Contributes to a robust and reliable testing infrastructure.

While other aspects, such as parallel execution, containerization, and reporting, are essential, ensuring the reliability and stability of your UI tests forms the foundation for a successful testing strategy. It fosters a testing environment that developers and testers can trust, enabling the rapid and dependable delivery of high-quality software.

5. Wrapping Up

As we wrap up our trip through testing Swing applications, it’s clear that testing isn’t just a step; it’s a journey to make software reliable. To nail testing in Swing UI, you need to get the basics right.

Things like having reliable and stable tests and using containers for a consistent setup are crucial. Running tests in parallel and connecting with code quality tools add extra layers to make sure our code not only works but is also easy to maintain.

UI testing is tricky, so smart handling of test data and creating flexible testing environments are key. Detailed test reports help us see what’s going on, promoting teamwork and smart decision-making.

Handling stuff like asynchronous operations, keeping an eye on things continuously, and connecting with test case management systems show the importance of a complete testing approach. Quickly fixing any shaky tests is a promise to keep our testing system strong and trustworthy.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 months ago

Hi Eleftheria, could you please “shift down 2 gears” in your wording style? The net content of this article is interesting, but I find it rather hard to read because of the language which is over the top for a technical article (same goes for your previous article on authorization).  E.g. “unravel the intricacies of testing Swing applications, demystify the synergy between AssertJ and JUnit” sounds way too poetic (and can almost be interpreted as sarcastic). Something like “look into the details of testing Swing applications, and how AssertJ and JUnit work together” would be more appropriate. I have the… Read more »

Back to top button