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...

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

Feign , an early declarative REST client, was initially part of the Spring Cloud Netflix stack and later rebranded as Spring Cloud OpenFeign . Before its introduction, crafting HTTP calls using RestTemplate involved repetitive code for each service interaction. With Feign, developers could simply define an interface with method contracts mirroring the service's endpoints. Behind the scenes, proxy magic generated a fully functional HTTP client, eliminating the need for boilerplate code . HTTP Interface (Spring Framework 6) The recent release of Spring Framework 6 integrated this declarative REST client as a native part of the core web framework in the form of the HTTP Interface . All the necessary components reside in the spring-web module, which is a transitive dependency for either the spring-boot-starter-web or spring-boot-starter-webflux modules. Currently, the WebFlux dependency is essential due to the HttpServiceProxyFactory , responsible for client gener...