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.
- 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
- 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-testingYou should see:
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.