This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • MRUnit Tutorial

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

Setup development environment

  1. Download the latest version of MRUnit jar from Apache website: For example if you are using the Hadoop version 1.0.3, download mrunit-x.x.x-incubating-hadoop2.jar.
  2. Include the jar in your IDE classpath. Also download the latest verison of mokito ( and JUnit jar and add them to your class path of development environment.

Writing MRUnit test cases

MRUnit testing framework is based on JUnit and it can test Map Reduce programs written on 0.20 , 0.23.x , 1.0.x version of Hadoop.


Code Block
public class SMSCDRMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

  private Text status = new Text();
  private final static IntWritable addOne = new IntWritable(1);

   * Returns the SMS status code and its count
  protected void map(LongWritable key, Text value, Context context)
      throws, InterruptedException {

    //655209;1;796764372490213;804422938115889;6 is the Sample record format
    String[] line = value.toString().split(";");
    // If record is of SMS CDR
    if (Integer.parseInt(line[1]) == 1) {
      context.write(status, addOne);

The corresponding Reducer code is


Run the test class as JUnit class and it will pass or fail the test depending upon if the mapper is correctly written or not.

Testing Counters

One common use of self-created Counter is to track malformed records in the input.


Code Block
public void testMapper() {
    mapDriver.withInput(new LongWritable(), new Text(
    //mapDriver.withOutput(new Text("6"), new IntWritable(1));
      assertEquals("Expected 1 counter increment", 1, mapDriver.getCounters()

When the CDR record is of non SMS type out counter should be incremented by Mapper class , we are checking this by assertion that it is really incremented by one.