服务接口:
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或任何其他可行的测试方法来测试它?示例代码将不胜感激。对于简单的模拟,将对象作为参数传递非常令人困惑。有人清楚地解释了测试服务层的最佳方法是什么?测试服务接口将被认为是单元测试还是集成测试?
发布于 2011-03-22 15:05:40
现在,假设您使用的是带注释的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);
}
}您可能还会发现this article很有用,特别是当您使用JUnit 3时。
有关JUnit 4的快速帮助,请访问Refer this。
希望这能有所帮助。
https://stackoverflow.com/questions/5354907
复制相似问题