????????е?Mock???
???????????? ???????[ 2016/2/26 13:55:25 ] ??????????в??? ???????
	???????????У?????????????????????????
	????????????????A??飬?????B??飬????????????A?????????????B??飬????????????????
	?????????????????н??е???????????????????????????????????в?????????????
	???????????????????????????????????????????
	????????Mock??????????а?к??????????????????
	??????У??????????????????????????????????Mock????????????磺JMockit??????Java?????÷?Χ??????
	??????У?Mock???????????????????????????Java???????????Python??????????磺?????Mock Server?????÷?Χ???????
	??????????????ò??????ν??Mock??????????У???????????????????????Service??Dao??????????????????spring?????????????????????????????????????ioc??????????????????set??get?????????????????????????????????????
	???????????Java???????????????£??????????????????????????????????????class??????????????????new?????????????????????????????????壬?????????????????д????????????????????????????????????????????????????????????£????????????д???????Mock?????Demo????????????????????????????
	???????????
	import com.qmock.exception.FieldNotFindException;
	import com.qmock.exception.FieldSetException;
	import com.qmock.exception.InjectDataException;
	import com.qmock.exception.TypeToMockException;
	public class QMock {
	/**
	* @author quqing
	* @param typeToInject
	* @param injectData
	* @return
	* @throws TypeToMockException
	* @throws InjectDataException
	* @throws FieldSetException
	* @throws InstantiationException
	* @throws IllegalAccessException
	* @throws ClassNotFoundException
	* @throws FieldNotFindException
	*/
	public static Object setFields(Class<?> typeToInject?? Map<String?? Object> injectData) throws TypeToMockException?? InjectDataException?? FieldSetException?? InstantiationException??
	IllegalAccessException?? ClassNotFoundException?? FieldNotFindException {
	if (typeToInject == null)
	throw new TypeToMockException("Exception in typeToMock is NUll");
	if (injectData == null)
	throw new InjectDataException("Exception in injectData is NUll");
	Set<String> keys = injectData.keySet();
	Object obj = Class.forName(typeToInject.getName()).newInstance();
	Field[] fields = obj.getClass().getDeclaredFields();
	// ???Mock???????????
	for (String key : keys) {
	for (int i = 0; i < fields.length; i++) {
	if (key.equals(fields[i].getName()))
	break;
	if (i == fields.length - 1)
	throw new FieldNotFindException("Exception in Field Not Find >> " + key);
	}
	}
	// ??????????
	for (int j = 0; j < fields.length; j++) {
	fields[j].setAccessible(true);
	if (null != injectData.get(fields[j].getName())) {
	try {
	fields[j].set(obj?? injectData.get(fields[j].getName()));
	} catch (Exception e) {
	throw new FieldSetException("Exception in FieldSet >> " + fields[j].getName());
	}
	}
	}
	return obj;
	}
	/**
	* @author quqing
	* @param clazz ???????????·????????
	* @param method ??????
	* @param body ??????
	* @throws CannotCompileException
	* @throws NotFoundException
	*/
	public static void setMethod(String clazz?? String method?? String body) throws CannotCompileException?? NotFoundException {
	ClassPool classPool = ClassPool.getDefault();
	CtClass ctClass = classPool.get(clazz);
	CtMethod ctMethod = ctClass.getDeclaredMethod(method);
	ctMethod.setBody(body);
	ctClass.toClass();
	}
	}
	????Service??
	public class User {
	public User() {}
	public User(Integer id?? String name) {
	this.id = id;
	this.name = name;
	}
	private Integer id;
	private String name;
	public int getId() {return id;}
	public void setId(Integer id) {this.id = id;}
	public String getName() {return name;}
	public void setName(String name) {this.name = name;}
	}
	public interface UserServ {
	public User getUser(Integer id);
	int getNum();
	String getStr();
	List<String> getList();
	Map<String?? String> getMap();
	}
	public class UserServImpl implements UserServ {
	private UserDAO dao;
	//  private User user;
	private int num;
	private String str;
	private List<String> list;
	private Map<String?? String> map;
	public User getUser(Integer id) {
	System.out.println("UserBusinessDelegate");
	return dao.getUser(id);
	}
	public int getNum() {
	return this.num;
	}
	public String getStr() {
	return this.str;
	}
	public List<String> getList() {
	return this.list;
	}
	public Map<String?? String> getMap() {
	return this.map;
	}
	}
	
??????
					
					???·???
App??С????H5?????????????????Щ??
2024/9/11 15:34:34?????????????????????????
2024/9/10 11:13:49P-One ???????????????????????????????????????
2024/9/10 10:14:12???????????????????????????
2024/9/9 18:04:26??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44
					
			
								
								
								
								
								
								
								
								
								
								
				
sales@spasvo.com