DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Testing a JSF UI Using Docker

Testing a JSF UI Using Docker

Take a look at how you can use Docker and Testcontainers to complete your UI testing in this simple tutorial.

Kai Winter user avatar by
Kai Winter
·
May. 13, 19 · Tutorial
Like (2)
Save
Tweet
Share
7.35K Views

Join the DZone community and get the full member experience.

Join For Free

In two previous posts, I wrote about how to test the persistence layer against a Dockerized database and how to test a Java EE application combined with a database both running in a Docker container. Both posts were backend-centric, this one is on the frontend side.

If you are new to Docker or Testcontainers I recommend reading the first post in this series in which Testcontainers gets explained a bit more in detail. 

What We Need:

  • A Docker host, local or remote

  • A Docker image with Wildfly installed

  • Arquillian to deploy to the Dockerized server

  • An Arquillian callback to start the Docker container and configure Arquillian to deploy to it

  • Testcontainers to manage the Docker container.

Overview

A custom Docker image is used which contains Wildfly 14 (kaiwinter/wildfly14-mgmt-user, available on Docker Hub). A management user account allows us to deploy to this server. The deployment is done by wildfly-arquillian-container-remote.

These are the rough steps when a test is started:

1. Unit test is started

2. Arquillian runs our callback

3. Our callback starts the Docker container and configures Arquillian

4. Arquillian deploys the result of the  @Deployment  method

5. Arquillian runs every  @Test  method

The use of Docker is transparent for the test. You can use any existing Arquillian test without changes.

Setup

The tricky part is the dynamic configuration of Arquillian to let it deploy to the Dockerized Wildfly. This is necessary because the server ports are exposed on random ports to the outside of the Docker container to allow the parallel use of multiple instances of the same image. So after the Docker container is running we need to tell Arquillian the exposed management port of Wildfly.

Arquillian is configured by the file arquillian.xml and it cannot be changed by an API dynamically (GitHub Issue). But we can register a org.jboss.arquillian.core.spi.LoadableExtension service (via META-INF/services), which can register a listener on the configuration process.

The listener starts the Docker container and configures Arquillian (see WildflyDockerExtension):

/**
 * Method which observes {@link ContainerRegistry}. Gets called by Arquillian at startup time.
 */
public void registerInstance(@Observes ContainerRegistry registry, ServiceLoader serviceLoader) {
  GenericContainer dockerContainer = new GenericContainer("kaiwinter/wildfly14-mgmt-user:latest")
                                          .withExposedPorts(9990);
  dockerContainer.start();
  configureArquillianForRemoteWildfly(dockerContainer, registry);
}


Test Example

The test looks like a common Arquillian test:

@RunWith(Arquillian.class)
@RunAsClient
public class UserViewTest {

   @Drone
   private WebDriver driver;

   @Deployment
   public static WebArchive createDeployment() {
      // ... WAR creation
   }

   /**
    * Tests if the xhtml page contains a table with five rows.
    */
   @Test
   public void tableContainsData() {
      String address = WildflyDockerExtension.baseUrl + "users.xhtml";
      driver.get(address);
      WebElement datatable = driver.findElement(By.className("ui-datatable-data"));

      List<WebElement> datatableRows = datatable.findElements(By.className("ui-widget-content"));
      assertEquals(5, datatableRows.size());
   }
}

This is the complete example: UserViewTest. 

Debugging the test

To see if the UI is deployed correctly, set a breakpoint in the test method and open WildflyDockerExtension.baseUrl with the URL of the webpage in a browser (address in the test example above). If the test completes, Testcontainers removes the Docker container so this URL is available only as long as the test is running. If you have problems creating the Arquillian- @Deployment  you may want to bundle your complete application.

Alternatives

  • PrimeFaces testing support for Arquillian: No Docker, uses an embedded Tomcat as container.

Here's a link to the complete example.

Docker (software) unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Master Spring Boot 3 With GraalVM Native Image
  • The Beauty of Java Optional and Either
  • Microservices 101: Transactional Outbox and Inbox
  • What’s New in Flutter 3.7?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: