Maven by Example

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="http://xml.weather.yahoo.com/ns/rss/1.0"
     xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
    <channel>
        <title>Yahoo! Weather - New York, NY</title>
        <link>http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/</link>
        <description>Yahoo! Weather for New York, NY</description>
        <language>en-us</language>
        <lastBuildDate>Sat, 10 Nov 2007 8:51 pm EDT</lastBuildDate>

        <ttl>60</ttl>
        <yweather:location city="New York" region="NY" country="US" />
        <yweather:units temperature="F" distance="mi" pressure="in" speed="mph" />
        <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" />
        <image>
            <title>Yahoo! Weather</title>

            <width>142</width>
            <height>18</height>
            <link>http://weather.yahoo.com/</link>
            <url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
        </image>
        <item>
            <title>Conditions for New York, NY at 8:51 pm EDT</title>

            <geo:lat>40.67</geo:lat>
            <geo:long>-73.94</geo:long>
            <link>http://us.rd.yahoo.com/dailynews/rss/weather/New_York__NY/</link>
            <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" />
            <description><![CDATA[
<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/33.gif" /><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 />
]]></description>
            <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>
        </item>
    </channel>
</rss>

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 and XML response from Yahoo! Weather.