EasyMock实践指南

EasyMock是个mock框架,用于动态产生mock对象,mock对象主要是用来模拟真实环境中的对象,原理上你应该为每个被依赖的接口准备mock对象,但那样实在太麻烦,无端的会产生很多mock类,所以EasyMock派上用场了!注意EasyMock2.0不能为非接口产生mock对象

EashMock的使用模式

1)记录

mockobj.someMethod();

2) 如果该方法有返回值,那么必须使用

EasyMock.expectLastCall().andReturn(xxxx);(不一定非得用这句话,但一定要有返回 值) xxx为被mock对象的someMethod()方法的期望返回值

3)回放(用于激活mock对象)

EasyMock.replay(mockobj);

实例如下:

package moxtv.central.service.impl;

import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

import junit.framework.TestCase;

import moxtv.central.dao.UploadMachineDao;

import moxtv.central.dao.UploadMachineJDBCDao;

import moxtv.central.po.UploadServer;

import org.easymock.EasyMock;

/**

* 上传机管理测试集.

*

* @author weip

* @time 2006-3-30 10:06:09

*

*/

public final class UploadMachineServiceTest extends TestCase {

/**

* 要测试的类.

*/

private UploadMachineServiceImpl uploadMachineService;

/**

* 测试对象依赖的对象

*/

private UploadMachineDao mockUploadMachineDao;

/**

* 测试对象依赖的对象

*/

private UploadMachineJDBCDao mockUploadMachineJDBCDao;

/**

*

* @param name

*

String

*/

public UploadMachineServiceTest(final String name) {

super(name);

}

/**

*

* @author weip

* @time 2006-3-30 10:10:18 (non-Javadoc)

* @see junit.framework.TestCase#setUp()

* @throws Exception

*

a

*

*/

protected void setUp() throws Exception {

super.setUp();

mockUploadMachineDao = (UploadMachineDao) EasyMock

.createMock(UploadMachineDao.class);

mockUploadMachineJDBCDao = (UploadMachineJDBCDao) EasyMock

.createMock(UploadMachineJDBCDao.class);

uploadMachineService = new UploadMachineServiceImpl();

uploadMachineService.setUploadMachineDao(mockUploadMachineDao);

uploadMachineService.setUploadMachineJDBCDao(mockUploadMachineJDBCDao);

}

/*

* protected void tearDown() throws Exception { super.tearDown(); }

*/

/**

* 测试登陆上传机管理首页.

*

* @author weip

* @throws Exception

*

a

* @time 2006-3-30 10:20:14

*/

public void testLoadMainPage() throws Exception {

UploadMachineVO vo = new UploadMachineVO ();

mockUploadMachineDao.query(vo );

EasyMock.expectLastCall().andReturn(new ArrayList());

mockUploadMachineDao.getTotalMachineCount();

EasyMock.expectLastCall().andReturn(new Integer(6));

EasyMock.replay(mockUploadMachineDao);

Map map = uploadMachineService.loadMainPage(vo );

assertNotNull(map);

assertNotNull(map.get("list"));

List list = (List) map.get("list");

assertEquals(0, list.size());

Integer sum = (Integer) map.get("sum");

assertNotNull(sum);

assertEquals(6, sum.intValue());

}

/**

* 测试获取上传机详情.

*

* @author weip

* @throws Exception

*

a

* @time 2006-3-30 11:14:33

*/

public void testGetMachine() throws Exception {

int serverId = 1;

mockUploadMachineDao.getMachine(serverId);

UploadServer us = createUploadServer();

EasyMock.expectLastCall().andReturn(us);

EasyMock.replay(mockUploadMachineDao);

UploadServer machine = uploadMachineService.getMachine(serverId);

assertEquals(1, machine.getId().intValue());

}

/**

* 测试添加上传机.

*

* @author weip

* @throws Exception

*

a

* @time 2006-3-30 10:09:42

*/

public void testAddMachine() throws Exception {

UploadServer server = createUploadServer();

mockUploadMachineDao.addMachine(server);

EasyMock.replay(mockUploadMachineDao);

uploadMachineService.addMachine(server);

EasyMock.verify(mockUploadMachineDao);

}

/**

* 测试删除上传机.

*

* @author weip

* @throws Exception

*

a

* @time 2006-3-30 10:55:28

*

*/

public void testDeleteMachines() throws Exception {

int[] serverids = new int[] {1 };

mockUploadMachineJDBCDao.deleteMachines(serverids);

EasyMock.replay(mockUploadMachineJDBCDao);

uploadMachineService.deleteMachines(serverids);

EasyMock.verify(mockUploadMachineJDBCDao);

}

/**

* 测试修改上传机.

*

* @author weip

* @throws Exception

*

a

* @time 2006-3-30 11:10:48

*/

public void testUpdateMachine() throws Exception {

mockUploadMachineDao.getMachine(1);

UploadServer destus = createUploadServer();

EasyMock.expectLastCall().andReturn(destus);

EasyMock.replay(mockUploadMachineDao);

UploadServer srcus = createUploadServer();

uploadMachineService.updateMachine(srcus);

EasyMock.verify(mockUploadMachineDao);

}

/**

* @author weip

* @time 2006-4-7 9:12:09

* @return UploadServer

*/

private UploadServer createUploadServer() {

UploadServer us = new UploadServer(new Integer(1));

Set set = new HashSet();

us.setDiskSet(set);

return us;

}

}

你可能感兴趣的:(DAO,框架,JUnit)