You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Spring Boot Test对Thymeleaf模板进行单元测试

解决Thymeleaf模板渲染单元测试缺少Template Resolver的问题

问题描述

生产环境中ThymeleafProcessor Bean可正常解析src/main/resources/templates/test.html模板,返回"Hello World";但单元测试时,TemplateEngine的TemplateResolvers集合为空,调用process方法直接返回模板文件名"test.html",无法完成渲染。

解决方案

方法1:使用Spring Boot自动配置(推荐)

你的测试类手动指定了classes参数,导致Spring Boot不会自动加载Thymeleaf的自动配置组件(包括默认的SpringResourceTemplateResolver)。只需移除@SpringBootTest的classes参数,让Spring Boot自动扫描并配置Thymeleaf相关Bean:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
class ThymeleafProcessorTest {

    @Autowired
    private ThymeleafProcessor thymeleafProcessor;

    @Test
    void doTest() {
        var result = thymeleafProcessor.process();
        assertEquals("Hello World", result);
    }
}

方法2:手动引入Thymeleaf自动配置类

如果必须指定classes参数,需要将Thymeleaf的自动配置类ThymeleafAutoConfiguration加入其中,确保Spring能初始化TemplateResolver:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = {
        ThymeleafProcessor.class,
        ThymeleafAutoConfiguration.class
})
class ThymeleafProcessorTest {

    @Autowired
    private ThymeleafProcessor thymeleafProcessor;

    @Test
    void doTest() {
        var result = thymeleafProcessor.process();
        assertEquals("Hello World", result);
    }
}

方法3:手动配置TemplateResolver和TemplateEngine

如果需要完全自定义测试环境的Thymeleaf配置,可以在测试类中手动创建SpringResourceTemplateResolver和TemplateEngine Bean:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(classes = {
        ThymeleafProcessor.class,
        ThymeleafProcessorTest.TestThymeleafConfig.class
})
class ThymeleafProcessorTest {

    @Autowired
    private ThymeleafProcessor thymeleafProcessor;

    @Configuration
    static class TestThymeleafConfig {
        @Bean
        public SpringResourceTemplateResolver templateResolver() {
            SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
            resolver.setResourceLoader(new DefaultResourceLoader());
            resolver.setPrefix("classpath:/templates/");
            resolver.setSuffix(".html");
            resolver.setTemplateMode("HTML");
            resolver.setCacheable(false); // 测试环境关闭缓存
            return resolver;
        }

        @Bean
        public TemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
            TemplateEngine engine = new TemplateEngine();
            engine.setTemplateResolver(templateResolver);
            return engine;
        }
    }

    @Test
    void doTest() {
        var result = thymeleafProcessor.process();
        assertEquals("Hello World", result);
    }
}

说明

  • 方法1是最简洁的方式,利用Spring Boot的自动配置机制,无需额外配置。
  • 方法2适合需要精准控制加载Bean的场景,确保Thymeleaf的核心配置被初始化。
  • 方法3适合需要自定义模板路径、缓存策略等测试场景。

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:52:45