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. HDU ACM 1066 Last non-zero Digit in N!

    #include<iostream> using namespace std; int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2}; ...

  2. phantomjs环境搭建已经运行

    1.下载phantomjs http://phantomjs.org/ 2.运行 新建phantomjs.bat,记得改目录路径 里面内容为: D:\java\phantomjs\phantomjs. ...

  3. oracle expdp和impdp使用例子

    情景: 由于生产需求,需要把rmtel用户的数据完全复制一份给rmtel_xzy,但排除rmtel用户 ('CAB_JJXPORT_TAB','T_SERVICEXX','TB_CROSSCONNEC ...

  4. js 中 字符串的转换

    JS 中将 number 转换为 string 我们比较熟悉,直接用toString() 的方法就可以了:那么将 string 转换成为 number 都有些什么方法呢?如下便简单的列举了一些,以便记 ...

  5. 闲扯 Javascript 03 时钟和QQ延时框

    时钟 : 所用到得图片  : 开启定时器 setInterval  间隔型 setTimeout  延时型 停止定时器 clearInterval clearTimeout 效果思路 获取系统时间 D ...

  6. java --对象流与对象的序列化

    对象流 ObjectInputStream ObjectOutputStream类分别是InputStream和OutputStream的子类,对象输出流使用writeObject(Object ob ...

  7. 01-Foundation简介、NSObject、copy、NSString

    目录: 一.Foundation常用类 二.Foundation简介 三.NSObject 四.NSString 回到顶部 一.Foundation常用类 1 NSObject.NSString.NS ...

  8. 软件project师的属性与发展

    工作近十年了.[软件project师] 一直是我职业 title 的中心词,仅仅是前面的修饰语在不断变化,从0基础.中级.高级到资深. 事实上 [软件project师] 是一个非常泛化的定义.工作现实 ...

  9. SQL Server截取字符串和处理中文技巧

    一 环境介绍 SQL  Server PRINT @@VERSION MicrosoftSQLServer2012-11.0.2100.60(X64) Feb10201219:39:15 Copyri ...

  10. hdu 1075 What Are You Talking About(字典树)

    刚学的字典树,代码写得很不熟练.写法上也没有什么特别的优化,就是以1A为第一目标! 可惜还是失败了. 少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A.不过感觉对字典树有了 ...