2

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.

Chris White
  • 29,949
  • 4
  • 71
  • 93
  • 2
    Any reason why you aren't using MRUnit's `MapperDriver` to test this? – Chris White Jan 23 '13 at 11:53
  • I used the MRUnit's MapDriver and with this there is no need to mock the configuration object, we can set conf with MapDriver.setConfiguration(conf). However I required to mock the key and value and run the test as mapDriver.withInputKey(key).withInputValue().withOutput()..runTest(); Thank you. – user1876832 Feb 08 '13 at 19:18
  • You lost me towards the end of your comment - can you update your question to what you current have, and list what is still causing you problems - thanks – Chris White Feb 09 '13 at 02:04

1 Answers1

2

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

Configuration conf = mapdriver.getConfiguration();
conf.set("Year","2013");
brucenan
  • 584
  • 9
  • 21