实际工作中,测试角色可能会遇到如下情况: 场景一:甲开发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; } }