Wednesday, May 9, 2012

Unit testing Mapper.

In this post, I am writing a simple unit test using JMock for mapper mentioned previously. Briefly, the test case is validating the key and value collected by the context object. In this test case, context object is mocked and hence expectations can be set on the object. Below code is self explanatory.
package com.mycompany.example3;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MapClassTest {

    private MapClass mapper;
    private Text text;
    private Mapper.Context context;
    private Mockery mockery = new JUnit4Mockery() {{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};

    @Before
    public void setUp() throws Exception {
        mapper = new MapClass();
        context = mockery.mock(Mapper.Context.class);
        text = new Text("01-Apr-12 00:00:00,026  INFO  [scheduler_Worker-4] com.mycompany.sync.DistributedXipQuartzJobHandler.executeInternal(97) - Lock was acquired by other xip instance Quartz-lock [checkReversals] at 2012-04-01T00:00:00.026-07:00");
    }

    @Test
    public void testMap() throws Exception {
        mockery.checking(new Expectations() {
            {
                oneOf(context).write(with(new Text("scheduler_Worker-4")), with(new IntWritable(1)));
            }
        });

        mapper.map(new LongWritable(1), text, context);

        mockery.assertIsSatisfied();
    }
}

No comments:

Post a Comment