[ 
https://issues.apache.org/jira/browse/MAHOUT-730?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13048536#comment-13048536
 ] 

Frank Scholten commented on MAHOUT-730:
---------------------------------------

The following works. Let's say we have the WordCountMapper Hadoop example. By 
removing the private modifier of the Text key we can inject a mock for testing:

{code}
public class WordCountMapper extends Mapper<LongWritable, Text, Text, 
IntWritable> {
  private final static IntWritable one = new IntWritable(1);

  Text key = new Text();

  public void map(LongWritable key, Text value, Context context) throws 
IOException, InterruptedException {
    String line = value.toString();
    StringTokenizer tokenizer = new StringTokenizer(line);
    while (tokenizer.hasMoreTokens()) {
      this.key.set(tokenizer.nextToken());
      context.write(this.key, one);
    }
  }
{code}

In the test we verify that our inserted mock key was set 4 times with the 
correct value and written to the context, in the expected order.

{code}
public class WordCountMapperTest {

  private Mapper.Context context;
  private WordCountMapper mapper;
  private InOrder inOrder;
  private IntWritable countOnce;

  @Before
  public void before() {
    context = mock(Mapper.Context.class);
    mapper = new WordCountMapper();
    mapper.key = mock(Text.class);
    inOrder = inOrder(mapper.key, context, mapper.key, context, mapper.key, 
context, mapper.key, context);
    countOnce = new IntWritable(1);
  }

  @SuppressWarnings("unchecked")
  @Test
  public void testMap() throws IOException, InterruptedException {
    String line = "Machine learning with Mahout";

    mapper.map(new LongWritable(1), new Text(line), context);

    assertCountedOnce("Machine");
    assertCountedOnce("learning");
    assertCountedOnce("with");
    assertCountedOnce("Mahout");
  }

  @SuppressWarnings("unchecked")
  private void assertCountedOnce(String word) throws IOException, 
InterruptedException {
    inOrder.verify(mapper.key).set(eq(word));
    inOrder.verify(context).write(eq(mapper.key), eq(countOnce));
  }
}
{code}

> Replace EasyMock with Mockito
> -----------------------------
>
>                 Key: MAHOUT-730
>                 URL: https://issues.apache.org/jira/browse/MAHOUT-730
>             Project: Mahout
>          Issue Type: Improvement
>    Affects Versions: 0.6
>            Reporter: Sebastian Schelter
>            Assignee: Sebastian Schelter
>
> I'd like to switch our mock library from EasyMock to Mockito. The later one 
> needs less code (and doesn't need to be "replayed") and it's more intuitive 
> to write tests with it in my experience. Switching shouldn't be a big effort.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to