Maven by Example
   - 4.11. Adding Unit Test Resources

4.11. Adding Unit Test Resources

A unit test has access to a set of resources which are specific to tests. Often you’ll store files containing expected results and files containing dummy input in the test classpath. In this project, we’re storing a test XML document for YahooParserTest named ny-weather.xml and a file containing expected output from the WeatherFormatter in format-expected.dat.

To add test resources, you’ll need to create the src/test/resources directory. This is the default directory in which Maven looks for unit test resources. To create this directory execute the following commands from your project’s base directory.

$ cd src/test
$ mkdir resources
$ cd resources

Once you’ve create the resources directory, create a file named format-expected.dat in the resources directory.

Simple Weather’s WeatherFormatterTest Expected Output. 

Current Weather Conditions for:
New York, NY, US

Temperature: 39
Condition: Fair
Humidity: 67
Wind Chill: 39

This file should look familiar. It is the same output that was generated previously when you ran the Simple Weather project with the Maven Exec plugin. The second file you’ll need to add to the resources directory is ny-weather.xml.

Simple Weather’s YahooParserTest XML Input. 

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather=""
    <title>Yahoo Weather - New York, NY</title>
    <description>Yahoo Weather for New York, NY</description>
    <lastBuildDate>Sat, 10 Nov 2007 8:51 pm EDT</lastBuildDate>

    <yweather:location city="New York" region="NY" country="US" />
    <yweather:units temperature="F" distance="mi" pressure="in"
    <yweather:wind chill="39" direction="0" speed="0" />
    <yweather:atmosphere humidity="67" visibility="1609"
      pressure="30.18" rising="1" />
    <yweather:astronomy sunrise="6:36 am" sunset="4:43 pm" />
      <title>Yahoo Weather</title>
      <title>Conditions for New York, NY at 8:51 pm EDT</title>
      <pubDate>Sat, 10 Nov 2007 8:51 pm EDT</pubDate>
      <yweather:condition text="Fair" code="33" temp="39"
                                date="Sat, 10 Nov 2007 8:51 pm EDT"/>
<img src="" /><br />
<b>Current Conditions:</b><br />
Fair, 39 F<BR /><BR />
<b>Forecast:</b><BR />
Sat - Partly Cloudy. High: 45 Low: 32<br />
Sun - Sunny. High: 50 Low: 38<br />
<br />
      <yweather:forecast day="Sat" date="10 Nov 2007" low="32" high="45"
                               text="Partly Cloudy" code="29" />

      <yweather:forecast day="Sun" date="11 Nov 2007" low="38" high="50"
                               text="Sunny" code="32" />
      <guid isPermaLink="false">10002_2007_11_10_20_51_EDT</guid>

This file contains a test XML document for the YahooParserTest. We store this file so that we can test the YahooParser without having to retrieve an XML response from Yahoo Weather.

Become a Member

Are you a current user of: