Mocking the context object in MR unit

2.5k Views Asked by At

I'm new to Hadoop and this is my first mapper program, which I'm unit testing through MR unit.

I'm passing the parameter(year) which I set through the config object

    Configuration config =new Configuration()           
    config.set("Year", "2012");
    Job job=new Job(config ,"Yearly");

My Mapper:

public void map(ImmutableBytesWritable row, Result values, Context context)throws IOException, InterruptedException 
{   
  Configuration conf = context.getConfiguration();
  String Year= conf.get("Year");
}

In the MR unit tests I'm mocking the context class along with the key , value

@Test
public void testMapper() throws IOException, InterruptedException
{
  context = mock(Mapper.Context.class);

  Configuration conf=mock(Configuration.class);
  when(conf.get("Year")).thenReturn("2012");
  when(context.getConfiguration()).thenReturn(conf);
  mapper.map(row, result, context);
}

However not able to get the value(Year) in the mapping, receiving null. Am I doing this correct or ,Is there a better way to test the mapping.

1

There are 1 best solutions below

0
On

You should get the configuration from mapdriver in your test code, like this:

Configuration conf = mapdriver.getConfiguration();
conf.set("Year","2013");