Skip to main content

Automating Thymeleaf UI Rendering Tests in Spring Boot Using Selenium

In this article, we'll walk through setting up an automated UI test that:

- Reads article data from a database
- Visits corresponding article pages
- Verifies if the Thymeleaf template renders expected content correctly

We'll use Spring Boot, Selenium and WebDriverManager to build this system.

Use Case

Our application stores articles in a database. Each article has a:

- Title
- Slug (used in the URL)
- Content

We want to automatically test whether each article is:

- Reachable at /articles/{slug}
- Displays the correct title and content


Project Setup

Set up a Spring Boot project using Spring Initializr with Web, JPA, Thymeleaf, and H2 dependencies, then add Selenium and WebDriverManager for automated UI testing.

Maven Dependencies: pom.xml



Define the Entity — Article.java



ArticleRepository.java



ArticleController.java — Thymeleaf-backed UI



article.html — Thymeleaf Template



error.html — Thymeleaf Template



ArticleUiTest.java — Selenium-based UI test

- App starts via @SpringBootTest
- H2 DB loads article from data.sql
- Test hits /articles/slug using ChromeDriver
- Selenium checks page title and content are correct


Enable schema auto-generation in application.properties



Create data.sql in src/main/resources/

To insert article data into H2 in-memory database automatically when your Spring Boot app starts, simply create a file named data.sql inside src/main/resources.

H2 Console URL

You can access the H2 Console (a web UI to view the in-memory database) by visiting this URL (http://localhost:8080/h2-console) in your browser while your Spring Boot app is running.

Test Article Pages in Browser (Manual Test)

Visit http://localhost:8080/articles/spring-boot-testing

You should see:

Repeat for other slugs like: /articles/thymeleaf-basics

Automatically Test Using Selenium (UI Automation Test)

Right-click "ArticleUiTest.java → Run", or use "./mvnw test" in the console to run the test.



Comment out options.addArguments("--headless=new"); to see the browser live during the test.

Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...