gpt4 book ai didi

How to unit test a Thymeleaf template using Spring Boot Test(如何使用Spring Boot测试对百里叶模板进行单元测试)

转载 作者:bug小助手 更新时间:2023-10-27 19:57:45 30 4
gpt4 key购买 nike



I am parsing a Thymeleaf template using the following bean:

我正在使用以下Bean解析Thymeleaf模板:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Service
public class ThymeleafProcessor {

private final TemplateEngine templateEngine;

@Autowired
public ThymeleafProcessor(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}

public String process() {
final var templateResolvers = templateEngine.getTemplateResolvers();
System.out.println(templateResolvers);// Collection with one SpringResourceTemplateResolver
String process = templateEngine.process("test.html", new Context());
System.out.println(process); //Prints "Hello World"
return process;
}
}

The template is located in src/main/resources/templates and looks like this:

该模板位于src/main/resources/templates中,看起来像这样:


Hello World

So far everything works fine.

到目前为止一切正常。


Now I want to write a unit test for the bean but this doesn't work:

现在,我想为Bean编写一个单元测试,但这不起作用:


import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.thymeleaf.TemplateEngine;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {
ThymeleafProcessor.class,
TemplateEngine.class,
})
@EnableConfigurationProperties
class ThymeleafProcessorTest {

@Autowired
private ThymeleafProcessor thymeleafProcessor;

@Autowired
private TemplateEngine templateEngine;


@Test
void doTest() {
System.out.println(templateEngine.getTemplateResolvers().size()); //this will return 0 in the test

var result = thymeleafProcessor.process();

assertEquals("Hello World", result);
}

}

Insetad of the expected String "Hello World" the String "test.html" is returned. The problem seems to be that no Template Resolver is available.

Insetad的预期字符串“Hello World”的字符串“test.html”返回。问题似乎是没有可用的模板解析程序。


How does the test need to be configured in order to test the Thymeleaf template?

为了测试Thymeleaf模板,需要如何配置测试?


更多回答
优秀答案推荐

Those thymeleaf related beans are auto-configured by ThymeleafAutoConfiguration which is enabled if any configuration class is annotated with @EnableAutoConfiguration.

那些与胸腺叶相关的Bean是由ThymeleafAutoConfiguration自动配置的,如果任何配置类使用@EnableAutoConfiguration注解,就会启用它。


@SpringBootApplication has this @EnableAutoConfiguration , so when you start the app in normal way , ThymeleafAutoConfiguration takes effect and define the related thymeleaf beans.

@SpringBootApplication有这个@EnableAutoConfiguration,所以当你以正常的方式启动应用程序时,ThymeleafAutoConfiguration会生效并定义相关的胸腺叶Bean。


But now in test , you just specify to use ThymeleafProcessor and TemplateEngine as the configuration class to start the spring context. Neither @SpringBootApplication nor @EnableAutoConfiguration involved and so ThymeleafAutoConfiguration does not take effect.

但现在在测试中,您只需指定使用ThymeleafProcessor和TemplateEngine作为配置类来启动Spring上下文。既不涉及@SpringBootApplication,也不涉及@EnableAutoConfiguration,因此ThymeleafAutoConfiguration不会生效。


You can fix it by explicitly import ThymeleafAutoConfiguration using @ImportAutoConfiguration :

您可以通过使用@ImportAutoConfiguration显式导入ThymeleafAutoConfiguration来修复它:


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


}

I also tidy up the codes a bit :

我还稍微整理了一下代码:



  • @ExtendWith(SpringExtension.class) is not required as it is already defined by @SpringBootTest

    @ExtendWith(SpringExtension.class)不是必需的,因为@SpringBootTest已经定义了它



  • Do not need to define TemplateEngine in @SpringBootTest as it will be defined by ThymeleafAutoConfiguration

    不需要在@SpringBootTest中定义TemplateEngine,因为它将由ThymeleafAutoConfiguration定义



  • Most probably @EnableConfigurationProperties is also not required.

    很可能也不需要@EnableConfigurationProperties。




更多回答

Works great, thank you very much Ken !

效果很好,非常感谢你,肯!

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com