[英]Using another object as parameter in Jmockit Mocked object
I'm new to JMockit and have successfully run a basic unit test using it. 我是JMockit的新手,并且已经成功使用它运行了基本的单元测试。 However, I'm stuck when attempting to mock a Spring LdapTemplate.
但是,在尝试模拟Spring LdapTemplate时遇到问题。 The problem seems to be with the LdapQuery that is used by the LdapTemplate.
问题似乎出在LdapTemplate使用的LdapQuery上。 Do I need to mock this as well?
我也需要嘲笑吗?
JUnit Test 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()));
}
}
AccountService AccountService
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;
}
}
}
(Leaving out the Account class since it should be self explanatory.) (省去了Account类,因为它应该是自解释的。)
If I replace mockLdapTemplate.search(query().base(anyString).where(anyString) .is("userA"), (AttributesMapper) any);
如果我替换了
mockLdapTemplate.search(query().base(anyString).where(anyString) .is("userA"), (AttributesMapper) any);
with mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any)
the test passes (which is what I expect, but this more or less tells me the problem is with the LdapQuery parameter). 使用
mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any)
通过测试(这是我所期望的,但这或多或少告诉我问题出在LdapQuery参数上)。
Thanks! 谢谢!
You already know the answer: the expectation should be recorded as 您已经知道答案了:期望应该记录为
mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any)
because this is the only mocked method being called from the unit under test. 因为这是被测单元唯一调用的模拟方法。 The argument matchers "any", "withNotNull()", etc. can only be used on calls to mocked methods, and
LdapQuery
was not mocked in the test. 的参数匹配器“任何”,“withNotNull()”等只能在调用方法嘲笑使用,
LdapQuery
没有在测试嘲笑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.