实际工作中,测试角色可能会遇到如下情况:

解决以上问题无非就是模块隔离、业务解耦,构造虚拟对象

关于 Mock 的实现方式,有白盒和黑盒两种,我的理解如下

说到这里,不得不提第一次接触 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;
    }
}

Dao 层

public interface UserDAO {
    public User getUser(Integer id);
    int getNum();
    String getStr();
    List<HashMap<String, String>> getList();
    Map<String, String> getMap();
}

public class UserDAOImpl implements UserDAO {
    private User user;
    private int num;
    private String str;
    private List<HashMap<String, String>> list;
    private Map<String, String> map;

    private void init() {
        HashMap<String, String> map = new HashMap<String, String>();
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        map.put("a", "test");
        list.add(map);
        this.list = list;
    }

    public User getUser(Integer id) {
        return this.user;
    }

    public int getNum() {
        return this.num;
    }

    public String getStr() {
        return this.str;
    }

    public List<HashMap<String, String>> getList() {
        init();
        return this.list;
    }

    public Map<String, String> getMap() {
        return this.map;
    }
}

测试类 - 普通

public class TestDemo {

    public static void main(String[] args) {
        Map<String, Object> injectMap = new LinkedHashMap<String, Object>();

        try {
            User user = new User();
            String body = "{user.setId(new Integer(66));user.setName(\"hehe\");return this.user;}";
            QMock.setMethod("com.qmock.demo.UserDAOImpl", "getUser", body);

            List<String> list = new ArrayList<String>();
            list.add("testList");

            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("a", "testMap");

            injectMap.put("user", user);
            injectMap.put("num", 88);
            injectMap.put("str", "test");
            injectMap.put("list", list);
            injectMap.put("map", map);
            UserDAO userDAO = (UserDAOImpl) QMock.setFields(UserDAOImpl.class,
                    injectMap);

            injectMap.clear();
            injectMap.put("dao", userDAO);
            injectMap.put("num", 88);
            injectMap.put("str", "test");
            injectMap.put("list", list);
            injectMap.put("map", map);
            UserServ userServ = (UserServImpl) QMock.setFields(UserServImpl.class,
                    injectMap);

            System.out.println(userDAO.getUser(1).getId());
            System.out.println(userDAO.getUser(1).getName());
            System.out.println(userDAO.getNum());
            System.out.println(userDAO.getStr());
            System.out.println(userDAO.getList());
            System.out.println(userDAO.getMap());
            System.out.println("#######################################");
            System.out.println(userServ.getUser(1).getId());
            System.out.println(userServ.getUser(1).getName());
            System.out.println(userServ.getNum());
            System.out.println(userServ.getStr());
            System.out.println(userServ.getList());
            System.out.println(userServ.getMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试类-JUnit

public class UserServImplTest {
    @SuppressWarnings("unused")
    private User user = new User();
    private UserDAO userDAO;
    private UserServ userServ;
    private List<String> list = new ArrayList<String>();
    private Map<String, String> map = new LinkedHashMap<String, String>();
    private Map<String, Object> injectMap = new LinkedHashMap<String, Object>();

    @Before
    public void setUp() throws Exception {
        userDAO = null;
        userServ = null;
        injectMap.clear();
        user = null;
        user = new User();
        list.clear();
        map.clear();
    }

    @Test
    public void testGetUser() {
        try {
            HashMap<String, String> map = new HashMap<String, String>();
            List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
            map.put("a", "test");
            list.add(map);
            StringBuffer body = new StringBuffer();

            body.append("{\njava.util.HashMap map = new java.util.HashMap();\njava.util.List list = new java.util.ArrayList();\nmap.put(\"a\", \"test\");\nlist.add(map);\nreturn list;\n}");
            QMock.setMethod("com.qmock.demo.UserDAOImpl", "getList", body.toString());
            userDAO = new UserDAOImpl();

            System.out.println(userDAO.getList());
            System.out.println(userDAO.getList().get(0));
            System.out.println(userDAO.getList().get(0).get("a"));

            assertEquals(list, userDAO.getList());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetNum() {
        try {
            injectMap.put("num", 66);
            userServ = (UserServImpl) QMock.setFields(UserServImpl.class, injectMap);
            assertEquals(66, userServ.getNum());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetStr() {
        try {
            injectMap.put("str", "test");
            userServ = (UserServImpl) QMock.setFields(UserServImpl.class, injectMap);
            assertEquals("test", userServ.getStr());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetList() {
        try {
            list.add("testList");
            injectMap.put("list", list);
            userServ = (UserServImpl) QMock.setFields(UserServImpl.class, injectMap);
            assertEquals(list, userServ.getList());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetMap() {
        try {
            map.put("a", "testMap");
            injectMap.put("map", map);
            userServ = (UserServImpl) QMock.setFields(UserServImpl.class, injectMap);
            assertEquals(map, userServ.getMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

目前市场上的 Mock 框架很多,个人倾向于使用 JMockit,轻量级、强大,你能想到的场景,基本上都支持


↙↙↙阅读原文可查看相关链接,并与作者交流