0

I am writing a JUnit test, which should test the title of an html page. This project is a SpringBoot, Thymeleaf starter project.

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;
}

}

The Error:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/pages/dashboard/dashboard.html"

Dir tree

invincibles04
  • 109
  • 3
  • 13

2 Answers2

1

You have to use
private final static String HTML_PATH = "/pages/dashboard/dashboard"; //no ".html"

Feru Maximus
  • 91
  • 1
  • 7
0

Can you try replacing this line of code

templateResolver.setPrefix("/resources/templates/");

with this one

templateResolver.setPrefix("templates/");
Periklis Douvitsas
  • 2,431
  • 1
  • 13
  • 14