少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

//这里是测试UserDao userDao=EasyMock.createMock(UserDao.class);这种形式的:

package com.abin.lee.mock;

public class User {
 private int id;
 private String userName;
 private String passWord;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getPassWord() {
  return passWord;
 }
 public void setPassWord(String passWord) {
  this.passWord = passWord;
 }
}




package com.abin.lee.mock;

public interface UserDao {
 User query(String id);
}




package com.abin.lee.mock;

public class UserDaoImpl implements UserDao{

 public User query(String id) {
  User user=null;
  if(id.equals("1")){
   user=new User();
   user.setId(1);
   user.setUserName("abin1");
   user.setPassWord("varyall1");
  }
  if(id.equals("2")){
   user=new User();
   user.setId(2);
   user.setUserName("abin2");
   user.setPassWord("varyall2");
  }
  return user;
 }

}





package com.abin.lee.mock;

public interface UserService {
 User query(String id);
}




package com.abin.lee.mock;

public class UserServiceImpl implements UserService{
 private UserDao userDao;
 public User query(String id){
  return this.userDao.query(id);
 }
 
 public UserDao getUserDao() {
  return userDao;
 }

 public void setUserDao(UserDao userDao) {
  this.userDao = userDao;
 }
 
}





测试代码:

package com.abin.lee.mock;

import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;

public class UserMock {
 @Test
 public void test(){
  User expectedUser=new User();
  expectedUser.setId(1);
  expectedUser.setUserName("abin1");
  expectedUser.setPassWord("varyall1");
  UserDao userDao=EasyMock.createMock(UserDao.class);
  EasyMock.expect(userDao.query("1")).andReturn(expectedUser);
  EasyMock.replay(userDao);
  UserServiceImpl service=new UserServiceImpl();
  service.setUserDao(userDao);
  User user=service.query("1");
  Assert.assertNotNull(user);
  Assert.assertEquals(1, user.getId());
  Assert.assertEquals("abin1", user.getUserName());
  Assert.assertEquals("varyall1", user.getPassWord());
  EasyMock.verify(userDao);
 }
}





测试方法2:

package com.abin.lee.mock;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Assert;
import org.junit.Test;

public class UsersMock {
 @Test
 public void test(){
  User expectedUser=new User();
  expectedUser.setId(2);
  expectedUser.setUserName("abin2");
  expectedUser.setPassWord("varyall2");
  IMocksControl mock=EasyMock.createNiceControl();
  UserDao userDao=mock.createMock(UserDao.class);
  EasyMock.expect(userDao.query("2")).andReturn(expectedUser);
  mock.replay();
  UserServiceImpl service=new UserServiceImpl();
  service.setUserDao(userDao);
  User user=service.query("2");
  Assert.assertNotNull(user);
  Assert.assertEquals(2, user.getId());
  Assert.assertEquals("abin2", user.getUserName());
  Assert.assertEquals("varyall2", user.getPassWord());
  mock.verify();
  mock.resetToNice();
  
 }

}


posted on 2012-11-27 22:40 abin 阅读(532) 评论(0)  编辑  收藏 所属分类: easyMock

只有注册用户登录后才能发表评论。


网站导航: