Skip to content

Commit

Permalink
Described how to handle message source text
Browse files Browse the repository at this point in the history
  • Loading branch information
4dz committed Feb 19, 2015
1 parent 161bfc2 commit 9f31cbf
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Include the latest release from Maven,
<dependency>
<groupId>com.connect-group</groupId>
<artifactId>thymeleaf-tdd</artifactId>
<version>1.0.2</version>
<version>1.0.4</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -71,6 +71,10 @@ So an empty JUnit test class appears as follows,
public class SampleUnitTest {
@Autowired
private ThymeleafTestEngine testEngine;
@Autowired
private TestMessageSource messageSource;

}


Expand All @@ -89,6 +93,8 @@ To begin with, imagine you have been supplied a HTML file by your frontend devel
<p>Sub text</p>
<p>More sub text</p>
<a href="http://www.example.com/">A Link</a>
<pre>Some i18n copy goes here</pre>
</body>
</html>

Expand Down Expand Up @@ -183,6 +189,8 @@ So what is being output? Lets take a look...
<p>Sub text</p>
<p>More sub text</p>
<a href="http://www.example.com/">A Link</a>
<pre>Some i18n copy goes here</pre>
</body>
</html>

Expand Down Expand Up @@ -215,6 +223,28 @@ OK so we can add the text in now - with the data-th-text tag.

Run the test again - Green bar! The test passes.

### Should Use Resource Bundle Text in Pre Section
Often the copy on the website will come from i18n resource bundles. These are usually handled by Spring MessageSource configurations.

Within Thymeleaf-TDD 1.0.4 you can test for these as well. As always in TDD, the test comes first.

@Test
public void shouldUseTextFromResourceBundle_WhenPreTagRefersToMessageSourceKey() throws Exception {
messageSource.givenMessageWithKey("my_resource_message", "Expected i18n copy");
HtmlElements tags = testEngine.process(HTML_PATH_IN_TEST_RESOURCES_FOLDER, model);
assertThat(tags.matching("body > pre"), isSingleElementThat(hasOnlyText("Expected i18n copy")));
}

Run the test, and it should fail because the original HTML copy is being returned

<pre>Some i18n copy goes here</pre>
Now we can add the Thymeleaf attribute to the HTML source.

<pre data-th-text="#{my_resource_message}">Some i18n copy goes here</pre>

Now when we run the test we get a 'green bar' - test has passed.

## A note on Hamcrest
These test assertions have been designed around the Hamcrest assertion model. For more information see http://hamcrest.org/JavaHamcrest/ and for a handy quick reference see http://www.marcphilipp.de/downloads/posts/2013-01-02-hamcrest-quick-reference/Hamcrest-1.3.pdf

Expand Down

0 comments on commit 9f31cbf

Please sign in to comment.