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/