Search code examples
javahadoopjunitmockitomapreduce

Unit Test MapReduce - Junit Mockito


I am new to writing test cases for Map Reduce and as I googled, I understood that MRUnit is deprecated and have to use Mockito. Could someone provide inspiration on testing mapreduce with Junit mockito as I couldn't find one. I could only see MRUnit test cases for mapreduce.


Solution

  • I am providing a sample test class here for mapper. Test for reducer can also be written in same fashion.

    @RunWith(MockitoJUnitRunner.class)
    public class SampleMapperTest {
    
        @Mock
        private Mapper.Context mockContext; // declare your mocks
    
        @Mock
        Counter mockCounter; // mocked hadoop counter
    
        SampleMapper mapper;
    
        @Before
        public void setUp() throws Exception {
           /*
            * mock and define your components here
            */
            mapper = new SampleMapper();
            doNothing().when(mockCounter).increment(anyLong());
        }
    
        @Test
        public void testMap() throws IOException, InterruptedException {
           /*
            * whatever you want to test you can write here in the verify statement
            */
            String line1 = "key_value";
            mapper.map(null, new Text(line1), mockContext);
            verify(mockCounter, times(1)).increment(1);
        }
    
        @After
        public void tearDown() throws Exception {
           /*
            * this will do the clean up part
            */
            verifyNoMoreInteractions(mockContext);
        }
    }
    

    I hope you get some insight from this and should be able to write your test now.