这是一个JavaProject,有关EasyMock用法详见本文测试用例

首先是用到的实体类User.java

package com.jadyer.model;

public class User {
private int id;
private String username;
private String password; /*-- 三个属性的setter和getter略 --*/ public User(){} public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
}

下面是用到的两个Dao类

package com.jadyer.dao;

import com.jadyer.model.User;

public interface UserDao {
public User load(String username); public void delete(String username); public User save(User user);
} /**
* 用到的两个Dao类
*/ package com.jadyer.dao; public interface BlogDao {
public void update(String username);
}

下面是用到的Service接口类

package com.jadyer.service;

import com.jadyer.model.User;

public interface UserService {
public User get(String username); public User list(String username); public void update(String username); public User insert(User user); public User login(String username, String password);
}

下面是用到的Service接口实现类

package com.jadyer.service.impl;

import com.jadyer.dao.BlogDao;
import com.jadyer.dao.UserDao;
import com.jadyer.model.User;
import com.jadyer.service.UserService; public class UserServiceImpl implements UserService {
private UserDao userDao;
private BlogDao blogDao; public UserServiceImpl(){} public UserServiceImpl(UserDao userDao){
this.userDao = userDao;
} public UserServiceImpl(UserDao userDao, BlogDao blogDao){
this.userDao = userDao;
this.blogDao = blogDao;
} @Override
public User get(String username) {
return userDao.load(username);
} @Override
public User list(String username) {
userDao.load("http://blog.csdn.net/jadyer");
userDao.delete(username);
return userDao.load(username);
} @Override
public void update(String username) {
blogDao.update(username);
userDao.delete(username);
} @Override
public User insert(User user) {
if(null != userDao.load(user.getUsername())){
throw new RuntimeException("用户[" + user.getUsername() + "]已存在");
}
return userDao.save(user);
} @Override
public User login(String username, String password) {
User user = userDao.load(username);
if(null == user){
throw new RuntimeException("用户[" + username + "]不存在");
}
if(!user.getPassword().equals(password)){
throw new RuntimeException("用户[" + user.getUsername() + "]密码错误");
}
return user;
}
}

最后是包含了EasyMock简单用法的测试用例

package com.jadyer.service;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Assert;
import org.junit.Test; import com.jadyer.dao.BlogDao;
import com.jadyer.dao.UserDao;
import com.jadyer.model.User;
import com.jadyer.service.UserService;
import com.jadyer.service.impl.UserServiceImpl; /**
* @see --------------------------------------------------------------------------------------------------------------
* @see Mock简介
* @see mock和stub的区别:mock关注的是交互,主要解决的问题是对象之间的交互,而stub关注的是状态
* @see EasyMock就是实现Mock对象的框架,Mock对象是用来对一些未实现关联对象的类进行测试的对象
* @see EasyMock框架在使用时只需要一个easymock-3.1.jar
* @see 下载地址为http://sourceforge.net/projects/easymock/files/
* @see --------------------------------------------------------------------------------------------------------------
* @see Mock生命周期
* @see 比如说UserService依赖于UserDao,如果UserDao没有实现,我们可以通过Mock来模拟UserDao的实现
* @see 1)record-->记录UserDao可能会发生的操作的结果,并记录下所有交互过程
* @see 2)replay-->创建UserServiceImpl和UserDao的关联并完成测试
* @see 3)verify-->验证交互关系是否正确
* @see --------------------------------------------------------------------------------------------------------------
* @see Mock创建方式
* @see 1)createMock-------->此时创建的mock对象在进行verify时仅仅检查关联方法是否正常完成调用,只要完成次数一致就认为测试通过
* @see 2)createNiceMock---->很少使用,略掉
* @see 3)createStrictMock-->与createMock不同的是,它还要验证关联方法的调用顺序,即verify时要同时验证完成调用的次数和调用顺序
* @see --------------------------------------------------------------------------------------------------------------
* @see 小结
* @see 还是以UserService依赖于UserDao为例
* @see 如果UserService开发完毕,但UserDao尚未开发或未开发完毕,这时又想测试UserService,那么就可以通过Mock方式来测试
* @see 方法为用EasyMock虚拟一个UserDao对象,然后在虚拟的UserDao上针对UserService方法所需的各方法进行声明其可能出现的调用结果
* @see 也就是声明UserDao各方法所需的传入参数和返回结果,最后再调用UserService中的方法(即内部调用了UserDao的方法)并验证返回结果
* @see 如果对UserService方法的Mock测试全部通过,则表明UserService是编写正确的
* @see 若实际应用中发现UserService发生错误,则说明UserDao出错,而非UserService出错
* @see 说白了,当我们发现所依赖的对象没有实现,而此时又想做测试的话,就可以考虑用Mock来做测试
* @see --------------------------------------------------------------------------------------------------------------
* @create Jul 3, 2013 9:06:14 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/
public class UserServiceTest {
/**
* 测试普通Mock的使用
*/
@Test
public void testMock(){
//创建DAO的Mock对象
UserDao dao = EasyMock.createMock(UserDao.class);
//进入record阶段:下面这行代码意思是-->当调用dao.load()方法且传入参数为Jadyer时,其返回值为user对象
User user = new User(2, "Jadyer", "hongyu");
//如果UserService.get()里面调用了两次dao.load(),那么这里就要指定其次数
//EasyMock.expect(dao.load("Jadyer")).andReturn(user).times(2);
EasyMock.expect(dao.load("Jadyer")).andReturn(user);
//进入replay阶段
EasyMock.replay(dao);
UserService service = new UserServiceImpl(dao);
User user22 = service.get("Jadyer");
Assert.assertNotNull(user22);
Assert.assertEquals(user22.getId(), user.getId());
Assert.assertEquals(user22.getUsername(), user.getUsername());
Assert.assertEquals(user22.getPassword(), user.getPassword());
//进入verify阶段
EasyMock.verify(dao);
} /**
* 测试StrictMock的使用
*/
@Test
public void testStrictMock(){
UserDao dao = EasyMock.createStrictMock(UserDao.class);
User user = new User(2, "Jadyer", "hongyu");
//关键在这里:必须把交互的所有过程都记录下来,也就是说dao方法被调用了几次,分别是调用的哪个方法,包括传的参数及返回值
//若UserServiceImpl.getTwice()中调用两次load()传入参数都是Jadyer,那么下面这两次的expect()操作便可写成一行
//EasyMock.expect(dao.load("Jadyer")).andReturn(user).times(2);
EasyMock.expect(dao.load("http://blog.csdn.net/jadyer")).andReturn(user);
//expectLastCall()是用来操作没有返回值的方法,此时要先执行dao中没有返回值的方法,然后再调用expectLastCall()方法
dao.delete("Jadyer");
EasyMock.expectLastCall();
EasyMock.expect(dao.load("Jadyer")).andReturn(user);
EasyMock.replay(dao);
UserService service = new UserServiceImpl(dao);
User user22 = service.list("Jadyer");
Assert.assertNotNull(user22);
Assert.assertEquals(user22.getId(), user.getId());
Assert.assertEquals(user22.getUsername(), user.getUsername());
Assert.assertEquals(user22.getPassword(), user.getPassword());
EasyMock.verify(dao);
} /**
* 使用MocksControl可以检查一组调用对象之间的关系
* 它比较适用于这种情景-->UserService中的某个方法依赖于UserDao和BlogDao等多个对象的情况
*/
@Test
public void testMocksControl(){
//也可以通过Control创建一组Mock对象,如EasyMock.createControl()
IMocksControl control = EasyMock.createStrictControl();
//这时创建的Mock对象就类似于EasyMock.createStrictMock(UserDao.class);
UserDao userDao = control.createMock(UserDao.class);
BlogDao blogDao = control.createMock(BlogDao.class);
blogDao.update("Jadyer");
EasyMock.expectLastCall();
userDao.delete("Jadyer");
EasyMock.expectLastCall();
//让MocksControl进行操作
control.replay();
new UserServiceImpl(userDao, blogDao).update("Jadyer");
//验证MocksControl中的所有mock调用
control.verify();
} /**
* 测试添加一个不存在的用户
*/
@Test
public void testInsertNotExistUser(){
//先做好准备工作
UserDao dao = EasyMock.createStrictMock(UserDao.class);
User user = new User(2, "Jadyer", "hongyu");
UserService service = new UserServiceImpl(dao);
//然后开始EasyMock的测试(先要保证用户不存在,所以要先andReturn(null))
EasyMock.expect(dao.load(user.getUsername())).andReturn(null);
EasyMock.expect(dao.save(user)).andReturn(user);
EasyMock.replay(dao);
User user22 = service.insert(user);
Assert.assertNotNull(user22);
Assert.assertEquals(user22.getId(), user.getId());
Assert.assertEquals(user22.getUsername(), user.getUsername());
Assert.assertEquals(user22.getPassword(), user.getPassword());
EasyMock.verify(dao);
} /**
* 测试添加一个存在的用户
* @see 为了查看效果,可以将expected=RuntimeException.class删去
*/
@Test(expected=RuntimeException.class)
public void testInsertExistUser(){
//先做好准备工作
UserDao dao = EasyMock.createStrictMock(UserDao.class);
User user = new User(2, "Jadyer", "hongyu");
UserService service = new UserServiceImpl(dao);
//然后开始EasyMock的测试(先要保证用户存在,所以要先andReturn(user))
EasyMock.expect(dao.load(user.getUsername())).andReturn(user);
EasyMock.expect(dao.save(user)).andReturn(user);
EasyMock.replay(dao);
User user22 = service.insert(user);
Assert.assertNotNull(user22);
Assert.assertEquals(user22.getId(), user.getId());
Assert.assertEquals(user22.getUsername(), user.getUsername());
Assert.assertEquals(user22.getPassword(), user.getPassword());
EasyMock.verify(dao);
} /**
* 测试用户登录成功
*/
@Test
public void testLoginSuccess(){
//同样先做好准备工作
UserDao dao = EasyMock.createStrictMock(UserDao.class);
User user = new User(2, "Jadyer", "hongyu");
UserService service = new UserServiceImpl(dao);
//开始测试
//指定测试时所要登录的用户名和密码,由于这里是要测试登录成功的情况,所以这里用户名密码就要与准备数据中的相同
String username = "Jadyer";
String password = "hongyu";
//先要保证用户存在,所以要先andReturn(user)
EasyMock.expect(dao.load(username)).andReturn(user);
EasyMock.replay(dao);
User user22 = service.login(username, password);
Assert.assertNotNull(user22);
Assert.assertEquals(user22.getId(), user.getId());
Assert.assertEquals(user22.getUsername(), user.getUsername());
Assert.assertEquals(user22.getPassword(), user.getPassword());
EasyMock.verify(dao);
} /**
* 测试用户登录失败(用户不存在)
* @see 为了查看效果,可以将expected=RuntimeException.class删去
*/
@Test(expected=RuntimeException.class)
public void testLoginFailNotExistUser(){
//同样先做好准备工作
UserDao dao = EasyMock.createStrictMock(UserDao.class);
UserService service = new UserServiceImpl(dao);
//开始测试
//指定测试时所要登录的是一个不存在的用户
String username = "Jaders";
String password = "hongyu";
//为了保证用户存在,这里就要andReturn(null),因为dao.load一个不存在用户时取到的是null
EasyMock.expect(dao.load(username)).andReturn(null);
EasyMock.replay(dao);
service.login(username, password);
EasyMock.verify(dao);
} /**
* 测试用户登录失败(用户密码错误)
* @see 为了查看效果,可以将expected=RuntimeException.class删去
*/
@Test(expected=RuntimeException.class)
public void testLoginFailPasswordError(){
//同样先做好准备工作
UserDao dao = EasyMock.createStrictMock(UserDao.class);
User user = new User(2, "Jadyer", "hongyu");
UserService service = new UserServiceImpl(dao);
//开始测试
//指定测试时所要登录的是一个密码错误的用户
String username = "Jadyer";
String password = "xuanyu";
//密码错误时用户是存在的,所以要andReturn(user)
EasyMock.expect(dao.load(username)).andReturn(user);
EasyMock.replay(dao);
service.login(username, password);
EasyMock.verify(dao);
}
}
05-07 15:19