Jmockit 初体验【版本1.6】

package com.paic.yxd.aps.biz;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Mocked;
import mockit.NonStrictExpectations;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.web.client.RestOperations;

import com.alibaba.fastjson.JSON;
import com.paic.pafa.exceptions.BusinessException;
import com.paic.yxd.aps.common.service.BaseServiceExt;
import com.paic.yxd.aps.service.insurance.biz.InsuranceDateService;
import com.paic.yxd.aps.service.insurance.biz.InsuranceService;
import com.paic.yxd.aps.service.insurance.biz.impl.InsuranceServiceImpl;
import com.paic.yxd.aps.service.insurance.dto.InsuranceDateDto;
import com.paic.yxd.aps.service.insurance.dto.InsuranceMoneyData;

public class InsuranceServiceTest extends BizBaseTest {
	
	@Resource(name = InsuranceService.SERVICE_ID)
	private InsuranceService insuranceService;
	
	//模拟私有方法
	@Mocked(value={"getTotalInsuredAmount"})
	InsuranceServiceImpl insuranceServiceImpl;
	
	@Mocked
	BaseServiceExt baseServiceExt;
	
	@Resource
	InsuranceDateService insuranceDateService;
	
	/**
	 * 测试预保单
	 * @Description
	 * @author hedong
	 * @date 2016-11-16 上午10:12:13
	 * @modifyNote 
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	@Test
	public void testGenerBeforehandInsurance() throws BusinessException{
		
		 new Expectations() {
			 {
				 /**
				  * 模拟私有方法
				  */
				Deencapsulation.invoke(insuranceServiceImpl,"getTotalInsuredAmount",anyString);
				InsuranceMoneyData insuranceMoneyData=new InsuranceMoneyData(1010, 1000, 10);
				result=insuranceMoneyData;
				times=1;
			}
			 {
				 //mock post方法
				 baseServiceExt.post((Map<String,Object>) withNotNull(), (RestOperations)withNotNull(), anyString);
				 Map<String,Object> resultMap=new HashMap<String,Object>();
				 Map<String,Object> insuranceResultMap=new HashMap<String,Object>();
				 insuranceResultMap.put("applyPolicyNo", "A001");
				 insuranceResultMap.put("noticeNo", "N001");
				 insuranceResultMap.put("totalInsuredAmount", "5000");
				 insuranceResultMap.put("totalActualPremium", "500");
				 insuranceResultMap.put("paymentEndDate", "2018-05-02");
				 resultMap.put("result", insuranceResultMap);
				 result=JSON.toJSONString(resultMap);
				 times=1;
			 }
		};
		
		Map<String, Object> param = getBeforInsuranceInputPara();
		
		Map<String, Object> map=insuranceService.generBeforehandInsurance(param);
		System.out.println(map);
		Assert.assertNotNull(map);
		
	}

	/**
	 * 预保单入参
	 * @Description
	 * @author hedong
	 * @date 2016-11-21 上午11:05:27
	 * @modifyNote 
	 * @return 预保单入参
	 */
	private Map<String, Object> getBeforInsuranceInputPara() {
		Map<String, Object> param=new HashMap<String, Object>();
		
		param.put("systemId", "好车主");
		param.put("systemFlowId", "12123");
		//测试环境11111   开发环境FDSAFG89428490328
		param.put("applicationNo", "test201600717970");
		param.put("personnelType", "1");
		param.put("name", "张三");
		param.put("certificateType", "01");
		param.put("certificateNo", "5100000000000000000X");
		param.put("linkManName", "张思");
		param.put("birthday", "2011-01-01");
		param.put("sexCode", "M");
		param.put("nationality", "中国");
		param.put("mobileTelephone", "18888888888");
		param.put("officeTelephone", "028-88888");
		param.put("homeTelephone", "18888888899");
		param.put("totalInsuredAmount", "10000");
		param.put("totalActualPremium", "100");
		param.put("expectRate", "0.1");
		
		List<Map<String,Object>> riskPropertyInfoList=new ArrayList<Map<String,Object>>();
		Map<String, Object> m1=new HashMap<String, Object>();
		m1.put("applyNum", "1");
		m1.put("applyNo", "1111");
		m1.put("productNo", "3000100010001");
		m1.put("applyCity", "盐亭县");
		m1.put("loanAmount", "10000");
		m1.put("loanInterest", "100");
		m1.put("loanTimeLimit", "12");
		riskPropertyInfoList.add(m1);
		
		param.put("riskPropertyInfoList", riskPropertyInfoList);
		return param;
	}
	
	/**
	 * 测试正式保单
	 * @Description
	 * @author hedong
	 * @date 2016-11-16 上午10:12:32
	 * @modifyNote
	 */
	@SuppressWarnings("unchecked")
	@Test
	public void testGenerateFormalInsurance(){
		
		new NonStrictExpectations(insuranceDateService) {
			{
				insuranceDateService.getInsuranceDateDto(anyString, anyString);
				result=new InsuranceDateDto(new Date(), new Date(), new Date(), new Date());
			}
			{
				//mock post方法
				 baseServiceExt.post((Map<String, Object>)any, (RestOperations)any, anyString);
				 result="{\"responseCode\":\"999999\",\"responseMsg\":\"成功\",\"result\":{\"applyPolicyNo\":\"52696023900014779010\",\"policyNo\":\"12696023900011008630\"}}";
			 }
			
		};
		insuranceService.generateFormalInsurance(null);
	}

}

复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值