Skip to content
Alistair Todd edited this page Apr 9, 2015 · 2 revisions

JUnit Rule for easy Freemarker template unit tests

See SampleTest.java for an example.

Using a JUnit Rule, easily load and process a Freemarker template against a model, get the output as

  • text
  • an HtmlPage for use with HtmlUnit
  • an XML Source allowing use of Hamcrest XML matchers to make assertions against the output

It's a simple as:

Declaring the rule to load a named template:

@Rule public FreemarkerTestRule template = new FreemarkerTestRule("src/test/java", "sample.ftl");

Then getting the html response for a given model, managed into xml:

Source theXml = template.xmlResponseFor(model);

Then making assertions against the xml:

assertThat(theXml, hasXPath("//td[@id='this-one']", containsString("Expected")));

Some further details at https://cleantestcode.wordpress.com/2014/06/01/unit-testing-freemarker-templates/

Clone this wiki locally