?????????????????е????????????????job?????????????????е???????????????????????????????????????????????????????????????map??reduce???????в????
????tomwhite???頂hadoop????????????????Mockito???е?????????????????????????????????wordcount???е???????(???????????????????????ο?????????????????)??
package org.apache.hadoop.examples;
/* author zhouhh
* date:2012.8.7
*/
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.io.*;
import org.junit.*;
public class WordCountTest {
@Test
public  void testWordCountMap() throws IOException?? InterruptedException
{
WordCount w = new WordCount();
WordCount.TokenizerMapper mapper = new WordCount.TokenizerMapper();
Text value = new Text("a b c b a a");
@SuppressWarnings("unchecked")
WordCount.TokenizerMapper.Context context = mock(WordCount.TokenizerMapper.Context.class);
mapper.map(null?? value?? context);
verify(context??times(3)).write(new Text("a")?? new IntWritable(1));
verify(context).write(new Text("c")?? new IntWritable(1));
//verify(context).write(new Text("cc")?? new IntWritable(1));
}
@Test
public void testWordCountReduce() throws IOException?? InterruptedException
{
WordCount.IntSumReducer reducer = new WordCount.IntSumReducer();
WordCount.IntSumReducer.Context context = mock(WordCount.IntSumReducer.Context.class);
Text key = new Text("a");
List values = new ArrayList();
values.add(new IntWritable(1));
values.add(new IntWritable(1));
reducer.reduce(key?? values?? context);
verify(context).write(new Text("a")?? new IntWritable(2));
}
public static void main(String[] args) {
//  try {
//   WordCountTest t = new WordCountTest();
//
//   //t.testWordCountMap();
//   t.testWordCountReduce();
//  } catch (IOException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  } catch (InterruptedException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  }
}
}
????verify(context)??????ε?д????????д??????verify(contex??times(n))??飬?????????
???????????????????run as JUnit Test????????????????????
???????????????hadoop1.0.3?????в????????Mockito???hadoop??lib????????????mockito-all-1.8.5.jar