MOCKITO 应用示例
package com.paic.wms.service.auditflow.impl;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.paic.mms.dto.PsPaicEmpInfoDTO;
import com.paic.wp.dao.system.AuditFlowDAO;
import com.paic.wp.service.system.AuditFlowServiceImpl;
//Let's import Mockitostatically so that the code looks clearer
import static org.mockito.Mockito.*;
//import staticorg.powermock.api.mockito.PowerMockito.*;
public class AuditFlowServiceImplTest {
AuditFlowServiceImpl service;
AuditFlowDAO daoMock;
@Before
public void setUp() throws Exception {
service = new AuditFlowServiceImpl();
daoMock = mock(AuditFlowDAO.class);
service.setAuditFlowDAO(daoMock);
}
@Test
public final void testSelectPsEmpInfoByUm() throws Exception {
testUmIsNull();
testUmIsNotNullAndPaPaicEmpInfoListIsNull();
testUmIsNotNullAndPaPaicEmpInfoListIsNotNull();
}
@Test
public final void testSelectPsEmpInfoByUmOrName() throws Exception {
testUmIsNullForByUmOrName();
testUmIsNotNullAndPaPaicEmpInfoListIsNullForByUmOrName();
testUmIsNotNullAndPaPaicEmpInfoListIsNotNullForByUmOrName();
}
@After
public void tearDown() throws Exception {
service = null;
}
private void testUmIsNotNullAndPaPaicEmpInfoListIsNotNull() throws Exception{
//given
String um = "zhengxinliang001";
List<PsPaicEmpInfoDTO> list = getPsPaicEmpInfoList();
//when
when(daoMock.selectPsEmpInfoByUm("zhengxinliang001")).thenReturn(list);
PsPaicEmpInfoDTO dto = service.selectPsEmpInfoByUm(um);
//then
assertEquals("郑新良", dto.getLAST_NAME());
}
private List<PsPaicEmpInfoDTO> getPsPaicEmpInfoList() {
PsPaicEmpInfoDTO dtoInServer = new PsPaicEmpInfoDTO();
dtoInServer.setPAIC_UM_NUM("zhengxinliang001");
dtoInServer.setLAST_NAME("郑新良");
dtoInServer.setDEPTID("10000000");
dtoInServer.setDEPT_NAME("平安机构");
List<PsPaicEmpInfoDTO> list = newArrayList<PsPaicEmpInfoDTO>();
list.add(dtoInServer);
return list;
}
private void testUmIsNotNullAndPaPaicEmpInfoListIsNull() throws Exception {
//given
String um = "noBody001";
//when
when(daoMock.selectPsEmpInfoByUm("noBody001")).thenReturn(null);
PsPaicEmpInfoDTO dto = service.selectPsEmpInfoByUm(um);
//then
assertEquals(dto, null);
}
private void testUmIsNull() throws Exception {
//given
String um = null;
//when
when(daoMock.selectPsEmpInfoByUm("")).thenReturn(null);
PsPaicEmpInfoDTO dto = service.selectPsEmpInfoByUm(um);
//then
assertEquals(dto, null);
}
//当um账号存在的时候
private void testUmIsNotNullAndPaPaicEmpInfoListIsNotNullForByUmOrName()throws Exception{
//given
String um = "zhengxinliang001";
//when
when(daoMock.selectPsEmpInfoByUmOrName(um)).thenReturn(getPsPaicEmpInfoList());
String umInfoFromService = service.selectPsEmpInfoByUmOrName(um);
//then
JSONObject umInfo = getPsEmpInfoJSON();
assertEquals(umInfo.toString(), umInfoFromService);
}
private JSONObject getPsEmpInfoJSON() {
JSONObject umInfo = new JSONObject();
umInfo.put("um", "zhengxinliang001");
umInfo.put("name", "郑新良");
umInfo.put("deptNo", "10000000");
umInfo.put("deptName", "平安机构");
JSONArray empArray = new JSONArray();
empArray.put(umInfo);
JSONObject returnJSON = new JSONObject();
returnJSON.put("size", 1);
returnJSON.put("zhengxinliang001", empArray);
return returnJSON;
}
//um账号不存在的时候
private void testUmIsNotNullAndPaPaicEmpInfoListIsNullForByUmOrName()throws Exception{
//given
String um = null;
//when
when(daoMock.selectPsEmpInfoByUm("noBody001")).thenReturn(null);
String umInfoFromService = service.selectPsEmpInfoByUmOrName(um);
//then
JSONObject umInfo = new JSONObject();
umInfo.put("size", "0");
assertEquals(umInfoFromService, umInfo.toString());
}
//当um账号是空的时候
private void testUmIsNullForByUmOrName() throws Exception {
//given
String um = null;
//when
when(daoMock.selectPsEmpInfoByUm("")).thenReturn(null);
String umInfoFromService = service.selectPsEmpInfoByUmOrName(um);
//then
JSONObject umInfo = new JSONObject();
umInfo.put("size", "0");
assertEquals(umInfoFromService, umInfo.toString());
}
}
MOCKITO 应用示例的更多相关文章
- Mockito常用方法及示例
Mockit是一个开源mock框架,官网:http://mockito.org/,源码:https://github.com/mockito/mockito 要使用Mockit,首先需要在我们工程中引 ...
- [译]Google官方关于Android架构中MVP模式的示例
概述 该示例(TODO-MVP)是后续各种示例演变的基础,它主要演示了在不带架构性框架的情况下实现M-V-P模式.其采用手动依赖注入的方式来提供本地数据源和远程数据源仓库.异步任务通过回调处理. 注意 ...
- JUnit + Mockito 单元测试(二)
摘自: http://blog.csdn.net/zhangxin09/article/details/42422643 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 入门 ...
- 转:Android官方MVP架构示例项目解析
转自: http://www.infoq.com/cn/articles/android-official-mvp-architecture-sample-project-analysis 作者 吕英 ...
- JavaEE参考示例 SpringSide 4.0 GA版杀青
SpringSide是以Spring Framework为核心的,Pragmatic风格的JavaEE应用参考示例,是JavaEE世界中的主流技术选型,较佳实践的总结与演示. 经过漫长的7个月和6个R ...
- [转载]5分钟了解Mockito
原文链接: http://liuzhijun.iteye.com/blog/1512780/ 5分钟了解Mockito 博客分类: Open SourceJava 一.什么是mock测试,什么是moc ...
- JUnit + Mockito 单元测试(二)(good)
import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mockito; import java.util.Lis ...
- Google官方MVP模式示例项目解析 todo-mvp
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6700668.html 引言:在Google没有给出一套权威的架构实现之前,很多App项目在架构方面都有或多 ...
- 单元测试系列:Mock工具之Mockito实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6780719.html 在实际项目中写单 ...
随机推荐
- Android的BUG(一) - HTML 5 播放streaming video造成卡住的问题
这个bug,是google自带的问题. 和见到的诸多android的疑难问题一样,这又是一个可以归类为 多线程同步/状态机 问题. 问题处在NuPlayer的异步消息的handle中,现象和原因不细说 ...
- c++ 重载,覆盖,重定义
写的不是很明白,后来又重新整理过了,在: http://www.cnblogs.com/iois/p/4986790.html 函数重载(Function Overloading) C++允许同一范围 ...
- MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件
原文 MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...
- 设计模式(五)适配器模式Adapter(结构型)
设计模式(五)适配器模式Adapter(结构型) 1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相 ...
- 基于visual Studio2013解决C语言竞赛题之0906文件插入
题目
- 业余写的一个播放器SDK,求点意见
好久没写博客了 现大致花了半年时间私下写一个音频SDK,想请csdn的达人提点意见,看看还需要增加哪些功能 我对这个的定位如下: 可以在游戏开发中播放音乐,作为一般的音频播放器后端,作为音频编辑器后端 ...
- 从汇编角度来理解linux下多层函数调用堆栈执行状态
注:在linux下开发经常使用的辅助小工具: readelf .hexdump.od.objdump.nm.telnet.nc 等,详细能够man一下. 我们用以下的C代码来研究函数调用的过程. C ...
- 基于集合成工控机Ubuntu系统安装分区详解
基于集合成工控机Ubuntu系统安装分区详解 硬件描述:双核的CPU,128G的固态硬盘 软件描述:使用Ubuntu12.04系统,内核3.8.0-29版本,QT4.8.1版本 1.新建分区表 /de ...
- hdu1151Air Raid
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- C中嵌入SQL
连接到SAMPLE数据库,查询LASTNAME为JOHNSON的FIRSTNAME信息. #include <stdio.h> #include <stdlib.h> #inc ...