I have the following two functions using the same LdapClient instance:
<code>private LdapName getPersonDn(String person) {
var r = ldapClient.search()
.query(q -> q.base("ou=people,o=comp").searchScope(SUBTREE)
.where("uid").is(person))
.toList(new DnContextMapper());
return r.isEmpty() ? null : r.getFirst();
private List<String> getPersonGroups(String personDn) {
return ldapClient.search()
.query(q -> q.base("ou=groups,o=comp").searchScope(SUBTREE).attributes("groupName")
.where("member").is(personDn))
.toList(new MyAttributeMapper());
<code>private LdapName getPersonDn(String person) {
var r = ldapClient.search()
.query(q -> q.base("ou=people,o=comp").searchScope(SUBTREE)
.where("uid").is(person))
.toList(new DnContextMapper());
return r.isEmpty() ? null : r.getFirst();
}
private List<String> getPersonGroups(String personDn) {
return ldapClient.search()
.query(q -> q.base("ou=groups,o=comp").searchScope(SUBTREE).attributes("groupName")
.where("member").is(personDn))
.toList(new MyAttributeMapper());
}
</code>
private LdapName getPersonDn(String person) {
var r = ldapClient.search()
.query(q -> q.base("ou=people,o=comp").searchScope(SUBTREE)
.where("uid").is(person))
.toList(new DnContextMapper());
return r.isEmpty() ? null : r.getFirst();
}
private List<String> getPersonGroups(String personDn) {
return ldapClient.search()
.query(q -> q.base("ou=groups,o=comp").searchScope(SUBTREE).attributes("groupName")
.where("member").is(personDn))
.toList(new MyAttributeMapper());
}
These two functions are called from another public function that collects the groups of which the given person is a member, but the actual implementation is not really relevant, let’s say it’s like:
<code>public List<String> getGroups(String person) {
var personDn = getPersonDn(person);
if (personDn == null) return List.of();
return getPersonGroups(personDn);
<code>public List<String> getGroups(String person) {
var personDn = getPersonDn(person);
if (personDn == null) return List.of();
return getPersonGroups(personDn);
}
</code>
public List<String> getGroups(String person) {
var personDn = getPersonDn(person);
if (personDn == null) return List.of();
return getPersonGroups(personDn);
}
I want to unit test this code, and for that I want to mock what LdapClient is going to return. Here’s my naive (not working) approach:
<code>private static final String KNOWN_UID = "foobar";
private static final String KNOWN_USER_DN = "uid=foobar,ou=people,o=comp";
@Mock(answer = DEEP_STUBS) private LdapClient ldapClient;
private PersonService personService;
var personDnQuery = LdapQueryBuilder.query().base("ou=people,o=comp").searchScope(SUBTREE)
.where("uid").is(KNOWN_UID);
when(ldapClient.search().query(eq(personDnQuery)).toList(DnContextMapper.class)
.thenReturn(List.of(LdapNameBuilder.newInstance(KNOWN_USER_DN).build());
void shouldReturnGroupsForUser() {
var groups = getGroups(KNOWN_USER);
<code>private static final String KNOWN_UID = "foobar";
private static final String KNOWN_USER_DN = "uid=foobar,ou=people,o=comp";
@Mock(answer = DEEP_STUBS) private LdapClient ldapClient;
private PersonService personService;
@BeforeEach
void setUp() {
var personDnQuery = LdapQueryBuilder.query().base("ou=people,o=comp").searchScope(SUBTREE)
.where("uid").is(KNOWN_UID);
when(ldapClient.search().query(eq(personDnQuery)).toList(DnContextMapper.class)
.thenReturn(List.of(LdapNameBuilder.newInstance(KNOWN_USER_DN).build());
...
}
@Test
void shouldReturnGroupsForUser() {
var groups = getGroups(KNOWN_USER);
...
}
</code>
private static final String KNOWN_UID = "foobar";
private static final String KNOWN_USER_DN = "uid=foobar,ou=people,o=comp";
@Mock(answer = DEEP_STUBS) private LdapClient ldapClient;
private PersonService personService;
@BeforeEach
void setUp() {
var personDnQuery = LdapQueryBuilder.query().base("ou=people,o=comp").searchScope(SUBTREE)
.where("uid").is(KNOWN_UID);
when(ldapClient.search().query(eq(personDnQuery)).toList(DnContextMapper.class)
.thenReturn(List.of(LdapNameBuilder.newInstance(KNOWN_USER_DN).build());
...
}
@Test
void shouldReturnGroupsForUser() {
var groups = getGroups(KNOWN_USER);
...
}
The rest is pretty much irrelevant at this stage. The issue is that, my mock returns null. I can get it working by NOT using the actual query that’s being used, but then my mock returns incorrect data.
How can I properly mock an LdapClient while making sure that the query used is what I expect it to be?