使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试

news/2024/7/4 9:59:31

0、preliminary 环境搭建

Setup development environment

Download the latest version of MRUnit jar from Apache website: https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/. For example if you are using the Hadoop version 1.0.3, download mrunit-x.x.x-incubating-hadoop2.jar.
Include the jar in your IDE classpath. Also download the latest verison of mokito (http://code.google.com/p/mockito/) and JUnit jar and add them to your class path of development environment.
请导入jar包到CentOS中的eclipse

download site: https://mrunit.apache.org/general/downloads.html
这里写图片描述

download web site https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/1.1.0/
这里写图片描述
这里写图片描述

You should Attention:

hadoop 框架包里面有自带的mock jar包,吧他们全部删除,否则要产生jar包兼容性异常(编译器不晓得调哪个jar包为好)


转自: http://www.infoq.com/cn/articles/HadoopMRUnit

(请关心里面的干货——测试用例文末)

引言

Hadoop MapReduce作业有着独一无二的代码架构,这种代码架构拥有特定的模板和结构。这样的架构会给测试驱动开发和单元测试带来一些麻烦。这篇文章是运用MRUnit,Mockito和PowerMock的真实范例。

我会介绍

使用MRUnit来编写Hadoop MapReduce应用程序的JUnit测试
使用PowerMock和Mockito模拟静态方法
模拟其他类型中的业务逻辑(译注:也就是编写测试驱动模块)
查看模拟的业务逻辑是否被调用(译注:测试驱动模块是否运行正常)
计数器
测试用例与log4j的集成
异常处理

本文的前提是读者应该已经熟悉JUnit 4的使用。

使用MRUnit可以把测试桩输入到mapper和/或reducer中,然后在JUnit环境中判断是否通过测试。这个过程和任何JUnit测试一样,你可以调试你的代码。MRUnit中的MapReduce Driver可以测试一组Map/Reduce或者Combiner。 PipelineMapReduceDriver可以测试Map/Reduce作业工作流。目前,MRUnit还没有Partitioner对应的驱动。MRUnit使开发人员在面对Hadoop特殊的架构的时候也能进行TDD和轻量级的单元测试。

测试用例(MapTest + ReduceTest + MapReduceTest)

publicclass MapTest {

private Mapper mapper;
private MapDriver driver;

@Before
publicvoid init(){
    mapper = new WordCount.Map();
    driver = new MapDriver(mapper);
}

@Test
publicvoid test() throws IOException{
    String line = "this is a test case for map";
    driver.withInput(new LongWritable(1),new Text(line))
            .withOutput(new Text("this"), new IntWritable(1))
            .withOutput(new Text("is"), new IntWritable(1))
            .withOutput(new Text("a"), new IntWritable(1))
            .withOutput(new Text("test"), new IntWritable(1))
            .withOutput(new Text("case"), new IntWritable(1))
            .withOutput(new Text("for"), new IntWritable(1))
            .withOutput(new Text("map"), new IntWritable(1))
            .runTest();
}

}

publicclass ReduceTest {

private Reducer reducer;
private ReduceDriver driver;

@Before
publicvoid init(){
    reducer = new WordCount.Reduce();
    driver = new ReduceDriver(reducer);

}
@Test
publicvoid test() throws IOException{
    String key = "test";
    List<IntWritable> values = new ArrayList();
    values.add(new IntWritable(2));
    values.add(new IntWritable(3));//5        
    driver.withInput(new Text(key),values)
            .withOutput(new Text("test"), new IntWritable(5))
            .runTest();
}

}

publicclass MapReduceTest {

private Reducer reducer;
private Mapper mapper;    
private MapReduceDriver driver;

@Before
publicvoid init(){
    reducer = new WordCount.Reduce();
    mapper = new WordCount.Map();
    driver = new MapReduceDriver(mapper,reducer);
}
@Test
publicvoid test() throws IOException{
    String line = "chinacache is a great CDN is it not";
    driver.withInput(new LongWritable(1),new Text(line))
            .withOutput(new Text("CDN"), new IntWritable(1))
            .withOutput(new Text("a"), new IntWritable(1))
            .withOutput(new Text("chinacache"), 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))
            .runTest();
}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/pacoson/p/4893182.html


http://www.niftyadmin.cn/n/2574514.html

相关文章

Powershell管理系列(十四)Exchange 2013邮箱数量统计

-----提供AD\Exchange\Lync\Sharepoint\CRM\SC\O365等微软产品实施及外包&#xff0c;QQ:185426445.电话18666943750管理Exchange的话&#xff0c;我们首先对自己管理的邮箱数量和分布情况有所了解。打开EAC我们确实很快就可以查到有多少邮箱数量&#xff0c;如果邮箱比较多的话…

还在找MCM模板?其实都内置了!用法见下

TeX live 2015及更新版本 和 MiKTeX &#xff08;以默认选项安装&#xff09;均内置了由Liam Huang维护的mcmthesis模板&#xff08;文档类&#xff09; 已被CTAN收录&#xff0c;可以说是全球认可的MCM模板了 所以&#xff0c;小伙伴们不要在找模板了&#xff0c;这个最好用 但…

LeetCode刷题 fIRST MISSING POSITIVE

Given an unsorted integer array,find missing postive integer. For example , Given [1,2,0]return 3, and [3,4,-1,1]return 2. Your algorithm should run in O(n) time and users constant space. 分析如下&#xff1a; 首先&#xff0c;要理解题解&#xff1a; 如果输入…

数据结构与算法--克鲁斯卡尔算法 最小生成树 Python详细实现克鲁斯卡尔算法 Python详细实现最小生成树

阅读目录基本概述克鲁斯卡尔算法图解克鲁斯卡尔算法分析Python代码实现补充知识点&#xff1a;如何获取int&#xff08;long&#xff09;和 float 的最值完整代码实现基本概述 先看一个应用场景&#xff1a; 克鲁斯卡尔算法介绍&#xff1a; 克鲁斯卡尔算法图解 还是以上面的…

应用 JD-Eclipse 插件实现 RFT 中 .class 文件的反向编译

概述 反编译是一个将目标代码转换成源代码的过程。而目标代码是一种用语言表示的代码 , 这种语言能通过实机或虚拟机直接执行。文本所要介绍的 JD-Eclipse 是一款反编译的开源软件&#xff0c;它是应用于 Eclipse 开发平台的插件&#xff0c;它可以帮助开发人员在调试程序的过程…

C语言作业小学生的算术题,C语言大型作业-教小学生算数.doc

..《C语言工程训练》课程设计题目&#xff1a; 教小学生算数学 生 学 号 &#xff1a;学 生 姓 名 &#xff1a;指 导 教 师 &#xff1a;需求分析通过此系统实现以下功能做个位数&#xff0c;十位数的加&#xff0c;减&#xff0c;乘和除&#xff0c;减法不能得负数&#xff0…

Linux常用功能脚本

设置定时任务crontab -e 1 0 * * * /bin/find /mnt/tomcat/logs/ -mtime 3 -type f -name "*.log" -exec /bin/rm -rf {} \; 每天凌晨一分定时清除Tomcat的日志脚本linux下Tomcat自动备份 1、页面文件在/home/edn/tomcat6/webapps目录下&#xff0c;备份文件存放在/h…

c语言中哪个字母不能在数字后面表示类型,C语言程序设计(40学时)-中国大学mooc-题库零氪...

第一周&#xff1a;程序设计与C语言1.1 计算机和编程语言随堂测验1、计算机本身最擅长的能力是&#xff1f;A、推理B、想像C、重复D、分析2、编程语言是和计算机交谈的语言3、计算机(CPU)可以直接运行人类编写的程序1.2 C语言随堂测验1、关于C语言&#xff0c;以下说法错误的有…