测试工作中的Mock实现
作者:网络转载 发布时间:[ 2016/2/26 13:55:25 ] 推荐标签:白盒测试 单元测试
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("{ java.util.HashMap map = new java.util.HashMap(); java.util.List list = new java.util.ArrayList(); map.put("a", "test"); list.add(map); return list; }");
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,轻量级、强大,你能想到的场景,基本上都支持,官网地址:http://jmockit.org/
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南