Back to Repositories

Testing Spring Boot Thymeleaf Web Components in spring-boot-examples

This test suite validates the core functionality of a Spring Boot Thymeleaf web application, focusing on endpoint responses, form submissions, and static resource serving. The tests ensure proper rendering of templates, successful form processing, and correct CSS delivery.

Test Coverage Overview

The test suite provides comprehensive coverage of essential web application features:
  • Homepage rendering and template processing
  • Form submission and response handling
  • Static resource (CSS) delivery
  • HTTP status code validation
  • Content verification for rendered pages

Implementation Analysis

The testing approach utilizes Spring Boot’s TestRestTemplate for HTTP interactions and assertions. Key patterns include:
  • Random port testing with WebEnvironment.RANDOM_PORT
  • REST endpoint verification
  • Form data submission using MultiValueMap
  • Response content validation using AssertJ assertions

Technical Details

Testing infrastructure includes:
  • JUnit 4 with SpringRunner
  • Spring Boot Test framework
  • TestRestTemplate for HTTP requests
  • AssertJ for assertions
  • LocalServerPort for dynamic port allocation
  • Automated test context configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test environment with random port assignment
  • Comprehensive endpoint testing
  • Clear test method naming conventions
  • Proper separation of concerns between tests
  • Effective use of Spring Boot testing utilities

ityouknow/spring-boot-examples

2.x/spring-boot-web-thymeleaf/src/test/java/com/neo/ThymeleafApplicationTests.java

            

package com.neo;

import java.net.URI;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ThymeleafApplicationTests {

	@Autowired
	private TestRestTemplate restTemplate;

	@LocalServerPort
	private int port;

	@Test
	public void testHome() {
		ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
		assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
		assertThat(entity.getBody()).contains("<title>Messages");
		assertThat(entity.getBody()).doesNotContain("layout:fragment");
	}

	@Test
	public void testCreate() {
		MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
		map.set("text", "FOO text");
		map.set("summary", "FOO");
		URI location = this.restTemplate.postForLocation("/", map);
		assertThat(location.toString()).contains("localhost:" + this.port);
	}

	@Test
	public void testCss() {
		ResponseEntity<String> entity = this.restTemplate.getForEntity(
				"http://localhost:" + this.port + "/css/bootstrap.min.css", String.class);
		assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
		assertThat(entity.getBody()).contains("body");
	}

}