- 引入MRUnit测试框架: pom.xml:
<!-- https://mvnrepository.com/artifact/org.apache.mrunit/mrunit MRUnit测试 -->
<dependency>
<groupId>org.apache.mrunit</groupId>
<artifactId>mrunit</artifactId>
<version>0.9.0-incubating</version>
<classifier>hadoop2</classifier>
<scope>test</scope>
</dependency>
2.编写测试类
public class TestReducer {
// 用于测试Reduce
@Test
public void TestMethod() {
ReduceDriver<Text, IntWritable, Text, IntWritable> reduceDriver = new ReduceDriver<Text, IntWritable, Text, IntWritable>();
//设置要测试对像
reduceDriver.withReducer(new MaxReduce());
// 设置测试的key=1950
reduceDriver.withInputKey(new Text("1950"));
// 设置测试的values=[ 10,13,0]
reduceDriver.withInputValues(Arrays.asList(new IntWritable(10), new IntWritable(13), new IntWritable(0)));
//设置预期输出结果
reduceDriver.withOutput(new Text("1950"), new IntWritable(134));
//执行测试
reduceDriver.runTest();
}
}
//要测试的程序
class MaxReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(key, new IntWritable(maxValue));
}
}