Back to Repositories

Testing Thymeleaf Template Integration in Spring Boot Examples

This test suite validates the Thymeleaf template integration within a Spring Boot web application. It focuses on testing HTTP endpoints, template rendering, and static resource handling using Spring’s TestRestTemplate.

Test Coverage Overview

The test suite provides comprehensive coverage of core web application functionality:

  • Homepage rendering and template processing
  • Form submission and POST request handling
  • Static resource serving (CSS files)
  • HTTP response validation
  • URL generation and port handling

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit4 integration. It employs @SpringBootTest with a random port configuration for isolated testing.

The TestRestTemplate is used to simulate HTTP requests, validating both response status codes and content. The implementation follows a clear pattern of request-response validation with specific assertions for content verification.

Technical Details

  • Spring Boot Test framework with JUnit4 runner
  • TestRestTemplate for HTTP request simulation
  • AssertJ for fluent assertions
  • Random port configuration for isolated testing
  • LinkedMultiValueMap for form data handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolation of test cases using random ports
  • Comprehensive validation of both HTTP status and response content
  • Clear test method naming conventions
  • Proper separation of concerns between different test scenarios
  • Efficient use of Spring Boot’s testing utilities

ityouknow/spring-boot-examples

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.http.HttpMethod;
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;

	private int port=8080;

	@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);

		ResponseEntity<String> response = restTemplate.exchange(
				createURLWithPort("/students/Student1/courses"),
				HttpMethod.POST, entity, String.class);
		assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
		assertThat(entity.getBody()).contains("body");
	}

	private String createURLWithPort(String uri) {
		return "http://localhost:" + port + uri;
	}

}