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


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 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 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);
	}
	
	
}



你可能感兴趣的:(JAVA)