Database Reference
In-Depth Information
Writing a Unit Test with MRUnit
The map and reduce functions in MapReduce are easy to test in isolation, which is a conse-
quence of their functional style. MRUnit is a testing library that makes it easy to pass
known inputs to a mapper or a reducer and check that the outputs are as expected. MRUnit
is used in conjunction with a standard test execution framework, such as JUnit, so you can
run the tests for MapReduce jobs in your normal development environment. For example,
all of the tests described here can be run from within an IDE by following the instructions
in Setting Up the Development Environment .
Mapper
The test for the mapper is shown in Example 6-5 .
Example 6-5. Unit test for MaxTemperatureMapper
import java.io.IOException ;
import org.apache.hadoop.io.* ;
import org.apache.hadoop.mrunit.mapreduce.MapDriver ;
import org.junit.* ;
public class MaxTemperatureMapperTest {
@Test
public void processesValidRecord () throws IOException ,
InterruptedException {
Text value = new
Text ( "0043011990999991950051518004+68750+023550FM-12+0382" +
// Year ^^^^
"99999V0203201N00261220001CN9999999N9-00111+99999999999" );
// Temperature ^^^^^
new MapDriver < LongWritable , Text , Text , IntWritable >()
. withMapper ( new MaxTemperatureMapper ())
. withInput ( new LongWritable ( 0 ), value )
. withOutput ( new Text ( "1950" ), new IntWritable (- 11 ))
. runTest ();
}
}
The idea of the test is very simple: pass a weather record as input to the mapper, and check
that the output is the year and temperature reading.
Since we are testing the mapper, we use MRUnit's MapDriver , which we configure with
the mapper under test ( MaxTemperatureMapper ), the input key and value, and the ex-
pected output key (a Text object representing the year, 1950) and expected output value
Search WWH ::




Custom Search