`

EastMock

 
阅读更多

EastMock实例

package test.mock.test01;

import static org.junit.Assert.*;

import org.easymock.EasyMock;
import org.junit.Test;
/**
 * EasyMock测试类
 *
 */
public class TestEastMock {

	@Test
	public void query() throws Exception{
		String name = "tom";

		User user = new User();
		user.setName("tom");
		
		//获取代理对象
		UserDao userDao = EasyMock.createMock(UserDao.class);
		//expect设置mock行为(要执行的方法及参数),andReturn设置调用该方法后返回对象
//		EasyMock.expect(userDao.query(name)).andReturn(user);
//		EasyMock.expect(userDao.query(name)).andReturn(user).times(3);//执行userDao.query(name)方法3次
		EasyMock.expect(userDao.query(name)).andThrow(new RuntimeException());//调用userDao.query(name)方法发生异常
		//测试时代替真实对象
		EasyMock.replay(userDao);
		
		
		UserServiceImpl userService = new UserServiceImpl();
		//设置mock对象
		userService.setUserDao(userDao);
		User resultDTO = userService.query(name);
		System.out.println("测试结果resultDTO:"+resultDTO);
		assertEquals(user, resultDTO);
		
	}
}

 

package test.mock.test01;

public interface UserDao {

	public User query(String name);
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics