Automating Thymeleaf UI Rendering Tests in Spring Boot Using Selenium

  • 4.4/5
  • 164
  • Jul 02, 2025
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.
Index
How to Implement PostgreSQL Full-Text Search with Spring Boot

15 min

Spring's transaction management with the @Transactional annotation

9 min

Spring Boot Rest APIs with PostgreSQL (Spring Boot + Rest APIs)

15 min

Caching in Spring Boot (@Cacheable, @CacheEvict & @CachePut)

21 min

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

13 min

A Guide to Pact Contract Testing in Spring Boot Applications

10 min

Circuit Breaker in Spring Boot (Spring Cloud Circuit Breaker + Resilience4j)

12 min

Handling Concurrent Service Calls in a Spring Boot Application: CompletableFuture and @Async

11 min

Profiling a Spring Boot application with Pyroscope

7 min

Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)

9 min

Dockerize Spring Boot app and Push image to DockerHub (Spring Boot + DockerHub)

4 min

Creating a Jenkins Pipeline for Spring Boot application

2 min

Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)

4 min

Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)

7 min

Edge Server Pattern in Microservices (Spring Cloud Gateway)

7 min

Spring Cloud config server setup with Git

8 min

Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)

9 min

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

24 min

Deploying Spring Boot microservices on Kubernetes Cluster

12 min

Reactive programming in Java with Project Reactor

50 min

Spring Reactive with PostgreSQL (Spring Boot WebFlux + PostgreSQL)

13 min

Spring Reactive, Thymeleaf Hello World (Spring Webflux + Thymeleaf + JS/CSS)

9 min

Problem JSON (application/problem+json) in Spring WebFlux

15 min

Spring Boot Login/Logout (Spring Security + MySql + Thymeleaf)

21 min

Securing Server-to-Server Communication with "Spring Boot" & "OAuth 2"

18 min

Integrating AWS OpenSearch with Spring Boot (Index, Search, Pagination & Aggregation)

8 min

Integrating Elasticsearch with a Spring Boot and PostgreSQL application

16 min

Sending Emails in Spring Boot via SMTP

7 min

How to create a basic Spring 6 project using Maven

5 min

Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)

9 min