kne*_*han 7 junit unit-testing easymock
服务接口:
public List<UserAccount> getUserAccounts();
public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions);
Run Code Online (Sandbox Code Playgroud)
服务实施:
public List<UserAccount> getUserAccounts() {
return getUserAccounts(null, null);
}
public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions) {
return getUserAccountDAO().getUserAccounts(resultsetOptions, sortOptions);
}
Run Code Online (Sandbox Code Playgroud)
如何使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可能还会发现本文很有用,特别是如果您使用 JUnit 3。
请参阅此处以获取有关 JUnit 4 的快速帮助。
希望有帮助。