???????????У?????????????????????????
????????????????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;
}
}