单元测试mockito

import com.alibaba.fastjson.JSON;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

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

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
public class IApiCommonPlsadmServiceImplTest {
    @InjectMocks
    IApiCommonPlsadmServiceImpl apiCommonPlsadmServiceImpl;

    @Mock
    IOperatePreQuotaDao operatePreQuotaDao;

    @Mock
    IMMDLoanInfoDao immdLoanInfoDao;

    @Mock
    IRunBatchLoanDao iRunBatchLoanDao;

    @Mock
    IMvData iMvData;

    @Mock
    IRxdContractCreateDao iRxdContractCreateDao;

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void queryQuotaStyleByIdsTest(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("cardNo","123");
        map.put("quotaType","123");
        String requestJson = JSON.toJSONString(map);
        PreCreditItemEntity entity = new PreCreditItemEntity();
        entity.setQuotaStyle("123");
        when(operatePreQuotaDao.queryPreCreditItemByMap(any(Map.class))).thenReturn(entity);
        apiCommonPlsadmServiceImpl.queryQuotaStyleByIds(requestJson);

        when(operatePreQuotaDao.queryPreCreditItemByMap(any(Map.class))).thenReturn(null);
        apiCommonPlsadmServiceImpl.queryQuotaStyleByIds(requestJson);

        try{
            when(operatePreQuotaDao.queryPreCreditItemByMap(any(Map.class))).thenThrow(new RuntimeException("123"));
            apiCommonPlsadmServiceImpl.queryQuotaStyleByIds(requestJson);
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Test
    public void queryLoanSettleFalgByIds(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("ids","aaaa");
        String requestJson = JSON.toJSONString(map);

        CustomerIdsRelationEntity customerIdsRelationEntity = new CustomerIdsRelationEntity();
        customerIdsRelationEntity.setCustomerNo("1111");
        customerIdsRelationEntity.setIds("aaaa");

        when(immdLoanInfoDao.queryCustomerRelationByParams(any(Map.class))).thenReturn(null);
        apiCommonPlsadmServiceImpl.queryLoanSettleFalgByIds(requestJson);

        when(immdLoanInfoDao.queryCustomerRelationByParams(any(Map.class))).thenReturn(customerIdsRelationEntity);
        when(iRunBatchLoanDao.queryRunBatchNewLoanMainCountByIds(any(Map.class))).thenReturn(1);
        apiCommonPlsadmServiceImpl.queryLoanSettleFalgByIds(requestJson);

        when(immdLoanInfoDao.queryCustomerRelationByParams(any(Map.class))).thenReturn(customerIdsRelationEntity);
        when(iRunBatchLoanDao.queryRunBatchNewLoanMainCountByIds(any(Map.class))).thenReturn(0);
        when(iMvData.getLoanMainDataByCustomerNo(any(Map.class))).thenReturn(null);
        apiCommonPlsadmServiceImpl.queryLoanSettleFalgByIds(requestJson);

        when(immdLoanInfoDao.queryCustomerRelationByParams(any(Map.class))).thenReturn(customerIdsRelationEntity);
        when(iRunBatchLoanDao.queryRunBatchNewLoanMainCountByIds(any(Map.class))).thenReturn(0);
        List<String> list= new ArrayList<>();
        list.add("111");
        list.add("222");
        when(iMvData.getLoanMainDataByCustomerNo(any(Map.class))).thenReturn(list);
        when(iRxdContractCreateDao.queryRxdRecorderCountByLoanNo(any(String.class))).thenReturn(1);
        apiCommonPlsadmServiceImpl.queryLoanSettleFalgByIds(requestJson);


 // 模拟调用方法返回值       
 when(immdLoanInfoDao.queryCustomerRelationByParams(any(Map.class))).thenReturn(customerIdsRelationEntity);
        when(iRunBatchLoanDao.queryRunBatchNewLoanMainCountByIds(any(Map.class))).thenReturn(0);
        when(iMvData.getLoanMainDataByCustomerNo(any(Map.class))).thenReturn(list);
        when(iRxdContractCreateDao.queryRxdRecorderCountByLoanNo(any(String.class))).thenReturn(0);
        apiCommonPlsadmServiceImpl.queryLoanSettleFalgByIds(requestJson);

        try{
 // 测试异常           
 when(immdLoanInfoDao.queryCustomerRelationByParams(any(Map.class))).thenThrow(new RuntimeException("123"));
            apiCommonPlsadmServiceImpl.queryLoanSettleFalgByIds(requestJson);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值