WordReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { if (containsTargetWord(key)) { int wordCount = 0; for (IntWritable value: values) { wordCount += value.get(); } context.write(key, new IntWritable(wordCount)); } }    }
@Test public void reduceWhenTargetWordIsFound() throws IOException, InterruptedException { Text key = new Text(WordReducer.TARGET_WORD); Iterable<IntWritable> values = createValues(1, 1, 1); reducer.reduce(key, values, contextMock); ArgumentCaptor<IntWritable> countArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(eq(key), countArgument.capture()); verifyNoMoreInteractions(contextMock); IntWritable count = countArgument.getValue(); assertEquals(3, count.get()); }
@Test public void reduceWhenTargetWordIsNotFound() throws IOException, InterruptedException { Text key = new Text(NOT_TARGET_WORD); Iterable<IntWritable> values = createValues(1, 1, 1); reducer.reduce(key, values, contextMock); verifyZeroInteractions(contextMock); }