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 应用示例的更多相关文章

  1. Mockito常用方法及示例

    Mockit是一个开源mock框架,官网:http://mockito.org/,源码:https://github.com/mockito/mockito 要使用Mockit,首先需要在我们工程中引 ...

  2. [译]Google官方关于Android架构中MVP模式的示例

    概述 该示例(TODO-MVP)是后续各种示例演变的基础,它主要演示了在不带架构性框架的情况下实现M-V-P模式.其采用手动依赖注入的方式来提供本地数据源和远程数据源仓库.异步任务通过回调处理. 注意 ...

  3. JUnit + Mockito 单元测试(二)

    摘自: http://blog.csdn.net/zhangxin09/article/details/42422643 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 入门 ...

  4. 转:Android官方MVP架构示例项目解析

    转自: http://www.infoq.com/cn/articles/android-official-mvp-architecture-sample-project-analysis 作者 吕英 ...

  5. JavaEE参考示例 SpringSide 4.0 GA版杀青

    SpringSide是以Spring Framework为核心的,Pragmatic风格的JavaEE应用参考示例,是JavaEE世界中的主流技术选型,较佳实践的总结与演示. 经过漫长的7个月和6个R ...

  6. [转载]5分钟了解Mockito

    原文链接: http://liuzhijun.iteye.com/blog/1512780/ 5分钟了解Mockito 博客分类: Open SourceJava 一.什么是mock测试,什么是moc ...

  7. JUnit + Mockito 单元测试(二)(good)

    import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mockito; import java.util.Lis ...

  8. Google官方MVP模式示例项目解析 todo-mvp

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6700668.html 引言:在Google没有给出一套权威的架构实现之前,很多App项目在架构方面都有或多 ...

  9. 单元测试系列:Mock工具之Mockito实战

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6780719.html 在实际项目中写单 ...

随机推荐

  1. 运行计划之误区,为什么COST非常小,SQL却跑得非常慢?

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/38321477 2014.7.31就晚20:30 My Oracle Support组猫大师 ...

  2. 自己定义progressdialog,改善用户体验

    自己定义progressdialog,改善用户体验 效果图: 详细实现方式: 1.定义Dialog的Layout布局 2.设置动画anim 3.创建自己定义dialog的样式 4.创建共同拥有方法来控 ...

  3. Adobe Acrobat Ⅺ Pro安装激活

    1.注意一定要断网安装,如果你有防火墙拦截亦可(注意:系统自带那防火墙不行). 2.将AcrobatPro_11_Web_WWMUI.exe解压到一个目录下,找到目录下的setup.exe安装,安装时 ...

  4. lvs、haproxy、nginx 负载均衡的比较分析

    lvs和nginx都可以用作多机负载的方案,它们各有优缺,在生产环境中需要好好分析实际情况并加以利用. 首先提醒,做技术切不可人云亦云,我云即你云:同时也不可太趋向保守,过于相信旧有方式而等别人来帮你 ...

  5. c#与java中byte字节的区别及转换方法

    原文:c#与java中byte字节的区别及转换方法 在java中  byte的范围在 [-128,127] 在C#中  byte的范围在 [0,255] 所以 java程序与C#程序 进行数据传输的时 ...

  6. android switch语句case expressions must be constant expressions

    在项目中遇到这样的Exception:case expressions must be constant expressions public class StandingCityActivity e ...

  7. MongoDB导出-导入-迁移

    linux环境下,将mongodb迁移到同机器,不同端口上. 命令参数: [mongodb@pera bin]$ ./mongodump --help Export MongoDB data to B ...

  8. keepalived 结合mysql 自动切换

    启动keepalived:/usr/local/sbin/keepalived -D -d -S 0 master ip:192.168.32.6 master:/root/sbin# cat /et ...

  9. mysql 监控 大批量的插入,删除,和修改

    监控大批量的插入,修改和删除: mysql> insert into aaa select * from aaa; mysql> SELECT trx_id, trx_state, trx ...

  10. haproxy path_beg

    path_beg : prefix match 前缀匹配 path_dir : subdir match path_dom : domain match path_end : suffix match ...