I recently was wondering if it was possible and a good idea to test resource bundles in Unit Tests.
Most likely it is not, but in our case we were checking the layout of log messages. To see if the messages lined up properly.
It only works if the properties file is available in the resources of the project. (in our case the properties files are all in an unrelated project (parent project), so it was either moving the unit test to a bad place, or moving the property files.)
public class InternationalisationTest
{
@BeforeClass
public void beforeClass() throws IOException
{
InputStream contentStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("texts_nl.properties");
assertThat(contentStream).isNotNull();
ResourceBundle rb = new PropertyResourceBundle(contentStream);
MessageUtilities.setResourceBundle(rb);
}
@AfterClass
public void afterClass()
{
MessageUtilities.resetResourceBundle();
}
@Test
public void testMessage()
{
assertThat(MessageUtilities.getMessage("file.not.found", "file.txt"))
.isEqualTo("Bestand 'file.txt' niet gevonden.");
}
}
{
@BeforeClass
public void beforeClass() throws IOException
{
InputStream contentStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("texts_nl.properties");
assertThat(contentStream).isNotNull();
ResourceBundle rb = new PropertyResourceBundle(contentStream);
MessageUtilities.setResourceBundle(rb);
}
@AfterClass
public void afterClass()
{
MessageUtilities.resetResourceBundle();
}
@Test
public void testMessage()
{
assertThat(MessageUtilities.getMessage("file.not.found", "file.txt"))
.isEqualTo("Bestand 'file.txt' niet gevonden.");
}
}
References
- Oracle JavaDoc - PropertyResourceBundle
- https://docs.oracle.com/javase/7/docs/api/java/util/PropertyResourceBundle.html
No comments:
Post a Comment