mardi 10 décembre 2019

Testing a Service that uses Thymeleaf standalone

I have a Spring Boot application that uses Thymeleaf as a standalone library to generate non-web content (emails, PDF). In other words, this isn't Spring MVC; I have a TemplateService that simply autowires the TemplateEngine and uses it directly. For example:

@Service
public class TemplateService {
  @Autowired
  private TemplateEngine templateEngine;

  public String buildXYZ() {
    Context c = new Context();
    c.setVariable("name", "Bob Smith");
    return templateEngine.process("templates/my-template.html", c);
  }
}

I would like to create tests for my TemplateService that ensure that I haven't forgotten to set any variables that are used by my template. For example, if my-template.html contains a reference to ${missingVar}, the test should fail because in the above code "missingVar" was never set as a variable on the Context.

The test should not hard-code the variables, since the templates can and do change. I've tried to find ways to intervene in Thymeleaf's variable-resolution mechanisms, but to no avail. Does anybody know a way to do this?

Aucun commentaire:

Enregistrer un commentaire