1.MRUnit
使用MRUnit框架进行单元测试,该框架对不同的测试对象使用不同的Driver,因此分为了:MapDriver、ReduceDriver和MapReduceDriver。
2. 代码:
(1)主要的包:
import org.apache.hadoop.mrunit.mapreduce.MapDriver; import org.apache.hadoop.mrunit.mapreduce.ReduceDriver; import org.junit.Rule; import org.junit.Test;
(2)代码主要部分:
public class MRJobTest { private MapDriver<LongWritable,Text, Text, Text> mapDriver = MapDriver.newMapDriver(new MRJob.MRJobMapper()); private ReduceDriver<Text, Text, Text, Text> reduceDriver = ReduceDriver.newReduceDriver(new MRJob.MRJobReducer()); @Test public void testMap() throws IOException { mapDriver.addInput(new LongWritable(), new Text( "2 4 101,10")); //输入数据 mapDriver.addInput(new LongWritable(), new Text( "3 5")); mapDriver.addInput(new LongWritable(), new Text( "")); assertEquals(7,mapDriver.run().size()); //执行map代码 } @Test public void testReducer() throws IOException { reduceDriver.addInput(new Text("1"), Lists.newArrayList(new Text("101"), new Text("101"), new Text("303"))); //输入reduce数据 reduceDriver.addInput(new Text("3"), Lists.newArrayList(new Text("101"), new Text("202"), new Text("303"))); assertEquals(3, reduceDriver.run().size()); //执行reduce.run代码 assertEquals("Expected 2 counter increment", 2, reduceDriver.getCounters() .findCounter(MRJob.MRJobCounter.LOW5).getValue()); //获取reduce中的自定义枚举名为MRJobCounter.LOW5的counter
} }