【发布时间】:2015-05-12 14:56:52
【问题描述】:
我是 JMockit 的新手,并且已经使用它成功地运行了一个基本的单元测试。但是,我在尝试模拟 Spring LdapTemplate 时遇到了困难。问题似乎出在 LdapTemplate 使用的 LdapQuery 上。我也需要模拟这个吗?
JUnit 测试
@RunWith(JMockit.class)
public class MyTest {
@Mocked
LdapTemplate mockLdapTemplate;
@Test
public void retrieveAccount_test() {
Account acct = new Account();
acct.setEmail("foobar@gmail.com");
acct.setUserId("userA");
final List<Account> expected = Arrays.asList(acct);
new Expectations() {
{
mockLdapTemplate.search(query().base(anyString).where(anyString)
.is("userA"), (AttributesMapper) any);
result = expected;
}
};
AccountService service = new AccountServiceImpl(mockLdapTemplate);
Account account = service.retrieveAccount("userA");
assertThat(account, is(notNullValue()));
}
}
账户服务
public class AccountServiceImpl implements AccountService {
private LdapTemplate ldapTemplate;
@Autowired
public AccountServiceImpl(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
@Override
public Account retrieveAccount(String userId) {
LdapQuery query = query().base("ou=users").where("uid").is(userId);
List<Account> list = ldapTemplate.search(query,
new AccountMapper());
if (list != null && !list.isEmpty()) {
return list.get(0);
}
return null;
}
public class AccountMapper implements
AttributesMapper<Account> {
@Override
public Account mapFromAttributes(Attributes attrs)
throws NamingException {
Account account = new Account();
account.setEmail((String) attrs.get("mail").get());
account.setUserId((String) attrs.get("uid").get());
return account;
}
}
}
(省略 Account 类,因为它应该是不言自明的。)
如果我将 mockLdapTemplate.search(query().base(anyString).where(anyString)
.is("userA"), (AttributesMapper) any); 替换为 mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any),则测试通过(这是我所期望的,但这或多或少告诉我问题出在 LdapQuery 参数上)。
谢谢!
【问题讨论】:
标签: java spring unit-testing junit jmockit