<dependency>
<groupId>com.cloudera.hadoop</groupId>
<artifactId>hadoop-mrunit</artifactId>
<version>0.20.2-320</version>
<scope>test</scope>
</dependency>
|
public class WordCountMapperTest {
private Mappermapper;
private MapDriverdriver;
@Before
public voidinit(){
mapper = newWordCountMapper();
driver = newMapDriver(mapper);
}
@Test
public voidtest() throws IOException{
String line ="Taobao is a great website";
driver.withInput(null,newText(line))
.withOutput(newText("Taobao"),new IntWritable(1))
.withOutput(newText("is"), new IntWritable(1))
.withOutput(newText("a"), new IntWritable(1))
.withOutput(newText("great"), new IntWritable(1))
.withOutput(newText("website"), new IntWritable(1))
.runTest();
}
}
|
public class WordCountReducerTest {
private Reducerreducer;
privateReduceDriver driver;
@Before
public voidinit(){
reducer = newWordCountReducer();
driver = newReduceDriver(reducer);
}
@Test
public voidtest() throws IOException{
String key ="taobao";
List values =new ArrayList();
values.add(newIntWritable(2));
values.add(newIntWritable(3));
driver.withInput(new Text("taobao"), values)
.withOutput(new Text("taobao"), new IntWritable(5))
.runTest();
}
}
|
public class WordCountTest {
private Mapper mapper;
private Reducer reducer;
private MapReduceDriver driver;
@Before
public void init(){
mapper = new WordCountMapper();
reducer = new WordCountReducer();
driver = new MapReduceDriver(mapper,reducer);
}
@Test
public void test() throws RuntimeException, IOException{
String line = "Taobao is a great website, is it not?";
driver.withInput("",new Text(line))
.withOutput(new Text("Taobao"),new IntWritable(1))
.withOutput(new Text("a"),new IntWritable(1))
.withOutput(new Text("great"),new IntWritable(1))
.withOutput(new Text("is"),new IntWritable(2))
.withOutput(new Text("it"),new IntWritable(1))
.withOutput(new Text("not"),new IntWritable(1))
.withOutput(new Text("website"),new IntWritable(1))
.runTest();
}
}
|
@Test
public void test() throws RuntimeException, IOException{
String line = "Taobao is a great website, is it not?";
List<Pair> out = null;
out = driver.withInput("",new Text(line)).run();
List<Pair> expected = new ArrayList<Pair>();
expected.add(new Pair(new Text("Taobao"),new IntWritable(1)));
expected.add(new Pair(new Text("a"),new IntWritable(1)));
expected.add(new Pair(new Text("great"),new IntWritable(1)));
expected.add(new Pair(new Text("is"),new IntWritable(2)));
expected.add(new Pair(new Text("it"),new IntWritable(1)));
expected.add(new Pair(new Text("not"),new IntWritable(1)));
expected.add(new Pair(new Text("website"),new IntWritable(1)));
assertListEquals(expected, out);
}
|