我正在编写一个JUnit测试,它应该测试html页面的标题。该项目是一个SpringBoot,Thymeleaf初级项目。
HtmlPath:
private final static String HTML_PATH = "/pages/dashboard/dashboard.html";
JUnitTest:
@Test
public void shouldRenderPageTitle() throws IOException, NodeSelectorException {
Map<String,Object> model = new HashMap<>();
model.put("pageTitle", "expected title");
HtmlElements tags = testEngine.process(HTML_PATH, model);
assertThat(tags.matching("title"), isSingleElementThat(hasOnlyText("expected title")));
}
ThymeleafConfiguration
@Configuration
public class ThymeleafConfig {
@Bean
public TemplateResolver templateResolver() {
TemplateResolver templateResolver = new TemplateResolver();
templateResolver.setPrefix("/resources/templates/");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new LayoutDialect());
templateEngine.addDialect(new SpringStandardDialect());
return templateEngine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
}
错误:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/pages/dashboard/dashboard.html"
答案 0 :(得分:1)
您必须使用
私有最终静态字符串HTML_PATH =“ / pages / dashboard / dashboard”; //没有“ .html”
答案 1 :(得分:0)
您可以尝试替换此行代码
templateResolver.setPrefix("/resources/templates/");
这一个
templateResolver.setPrefix("templates/");