我试图测试这个工作流程
public List<UserOrganization> getUserOrganizations(String username) {
try {
LOGGER.debug("Getting organizations for user {}", username);
ILdapUser ldapUser = ldapUserRest.getUserByUsername(username);
if (ldapUser == null || StringUtils.isEmpty(ldapUser.getCccUniqueId())) {
LOGGER.debug("Could not get ldap user for user name {}", username);
return new ArrayList<>();
}
....
所以我创建了这个测试
@Test
public void testGetUserOrganizationLdapUserNullFlow() throws Exception {
expect(ldapUserRest.getUserByUsername(USER_NAME)).andReturn(null);
expectNew(ArrayList.class).andReturn(new ArrayList<UserOrganization>());
replayAll();
userOrganizationService.getUserOrganizations(USER_NAME);
verifyAll();
}
对于我的测试类,我添加了注释@PrepareForTest
@PrepareForTest(ArrayList.class)
public class UserOrganizationServiceTest {
但是当我尝试运行它时,我发现了一个错误
但是,当我使用replay(ldapUserRest)
代替replayAll
时,测试工作正常。 (当我使用replayAll
而不是replay
使用模拟参数时,我的大多数测试都失败了。