单元测试--测试类的私有方法--反射


package com.paic.claimppt.common.biz.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.claimppt.common.biz.service.IdentifyingCodeService;
import com.paic.claimppt.common.dto.UserLoginInfoDTO;
import com.paic.claimppt.common.integration.sao.SvcsSAO;
import com.paic.claimppt.common.util.JsonUtils;
import com.paic.claimppt.common.util.PptPropsUtils;
import com.paic.claimppt.common.util.StringUtils;
import com.paic.claimppt.core.exception.ClaimpptBusinessException;
import com.paic.claimppt.core.util.CollectionUtils;
import com.paic.claimppt.core.util.Constants;
import com.paic.claimppt.core.util.LogUtils;
import com.paic.claimppt.usermanage.biz.service.LoginService;
import com.paic.pafa.app.biz.service.BaseService;


@Component("identifyingCodeService")
public class IdentifyingCodePojoService extends BaseService implements
		IdentifyingCodeService {

	@Autowired
	private SvcsSAO svcsSAO;

	@Autowired
	private LoginService loginService;

	// 方案接入号
	private String getSvcsPlanNo() {
		return PptPropsUtils.getValueFromProperties("asp_svcs.svcsPlanNo");
	}

	// 是否打开asp_svcs验证码功能
	private boolean isOpenVerifyIdentifyingCodeFilter() {
		if ("true"
				.equals(PptPropsUtils
						.getValueFromProperties("asp_svcs.isOpenVerifyIdentifyingCodeFilter"))) {
			return true;
		}
		return false;
	}

	// 判断用户是否需要验证(排除运营监控用户)
	private boolean isNeedVerifyUser(String userID) {
		boolean isNeedVerify = true;
		String excludeUsers = PptPropsUtils
				.getValueFromProperties("asp_svcs.excludeUsers");
		if (StringUtils.isNotEmpty(userID)
				&& StringUtils.isNotEmpty(excludeUsers)) {
			List<String> excludeUserList = Arrays.asList(excludeUsers
					.split(","));
			if (excludeUserList != null) {
				for (String excludeUser : excludeUserList) {
					if (userID.equalsIgnoreCase(excludeUser)) {
						isNeedVerify = false;
						break;
					}
				}
			}
		}
		return isNeedVerify;
	}

}



单测类; 

package com.paic.claimppt.common.biz.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.paic.claimppt.core.exception.ClaimpptBusinessException;
import com.paic.pafa.testing.BaseAppTests;

public class IdentifyingCodePojoServiceTest extends BaseAppTests {
	
	@Autowired
    private IdentifyingCodePojoService identifyingCodePojoService;

	/**
	 * Test method for {@link com.paic.claimppt.common.biz.service.impl.IdentifyingCodePojoService#getIdentifyingCode(java.lang.String)}.
	 * 此单测可以测试服务方验证码是否打开
	 */
	@Test
	public void testGetIdentifyingCode() {
		String userIP = "10.13.217.19";   //单测时需要将IP地址设置为IpV4
        try {
            Map<String, Object> returnMap = identifyingCodePojoService.getIdentifyingCode(userIP);
            System.out.println(returnMap);
        } catch (ClaimpptBusinessException e) {
            fail("获取验证码异常");
        }
        assertTrue("获取验证码成功", true);
	}
	
	@SuppressWarnings({"rawtypes","unchecked"})
	@Test
	public void testIsNeedVerifyUser_ExpectTrue() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		IdentifyingCodePojoService clazz = new IdentifyingCodePojoService();
		Class class1 = clazz.getClass();  
		Method isNeedVerifyUser = class1.getDeclaredMethod("isNeedVerifyUser", String.class);  
		isNeedVerifyUser.setAccessible(true);
		boolean bool = (Boolean) isNeedVerifyUser.invoke(clazz, "MINR1UI833");
		Assert.assertEquals("",true,bool);
		
	}
	
	@SuppressWarnings({"rawtypes","unchecked"})
	@Test
	public void testIsNeedVerifyUser_ExpectFalse() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		IdentifyingCodePojoService clazz = new IdentifyingCodePojoService();
		Class class1 = clazz.getClass();  
		Method isNeedVerifyUser = class1.getDeclaredMethod("isNeedVerifyUser", String.class);  
		isNeedVerifyUser.setAccessible(true);
		boolean bool = (Boolean) isNeedVerifyUser.invoke(clazz, "MINRUI833");
		Assert.assertEquals("",bool,false);
	}
	
	
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值