使用easymock在spring mvc中进行服务层测试
服务接口:
public List<UserAccount> getUserAccounts();
public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions);
服务实施:
public List<UserAccount> getUserAccounts() {
return getUserAccounts(null, null);
}
public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions) {
return getUserAccountDAO().getUserAccounts(resultsetOptions, sortOptions);
}
我如何使用easymock或任何其他可行的测试方法来测试它? 示例代码将不胜感激。 对于容易的模拟传递对象作为参数非常混乱。 有人清楚地解释什么是测试服务层的最佳方法? 测试服务接口将被视为单元测试还是集成测试?
假设您使用带注释的JUnit 4,请继续阅读:
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
public class UserAccountServiceTest
private UserAccountServiceImpl service;
private UserAccountDAO mockDao;
/**
* setUp overrides the default, We will use it to instantiate our required
* objects so that we get a clean copy for each test.
*/
@Before
public void setUp() {
service = new UserAccountServiceImpl();
mockDao = createStrictMock(UserAccountDAO.class);
service.setUserAccountDAO(mockDao);
}
/**
* This method will test the "rosy" scenario of passing a valid
* arguments and retrieveing the useraccounts.
*/
@Test
public void testGetUserAccounts() {
// fill in the values that you may want to return as results
List<UserAccount> results = new User();
/* You may decide to pass the real objects for ResultsetOptions & SortOptions */
expect(mockDao.getUserAccounts(null, null)
.andReturn(results);
replay(mockDao);
assertNotNull(service.getUserAccounts(null, null));
verify(mockDao);
}
}
如果您使用的是JUnit 3,您可能还会发现这篇文章很有用。
参考这个JUnit 4的快速帮助。
希望有所帮助。
链接地址: http://www.djcxy.com/p/51539.html