'Test verifications on embedded Spring LDAP server
we are using the default embedded ldap server in spring for our integration tests and configured it as follows:
spring:
ldap:
embedded:
base-dn: dc=XXX,dc=de
ldif: classpath:eap-schema.ldif
port: 34321
validation:
enabled: false
Everything works fine with that configuration in our tests.
We now implemented a caching of the user details provided by the ldap server after authentication. I would like to verify now that if we make to calls to our "userinfo" endpoint, the LDAP server is only called once by our application. I worked a lot with wiremock and i can get all events on a wiremock server pretty easy. I searched a lot but i could not find anything regarding this in the embedded ldap.
So has anyone an idea how i can verify that the ldap server was only called once in a @SpringBootTest integration test?
Thanks in advance
Solution 1:[1]
If you can abstract the codes that interact with LDAP server to a separate class or interface (e.g. LdapClient) , you can easily use traditional mocking technique such as using Mockito to verify the codes that you want to test only call the LdapClient (i.e. LDAP Server) one time. You even do not need to use the "LDAP wiremock server".
So suppose the codes that you want to test is getUserInfo() in the following UserService class :
public class UserService {
private LdapClient ldapClient;
private Map<String, User> userCache = new HashMap<>();
private UserService(LdapClient ldapClinet) {
this.ldapClient = ldapClinet;
}
public User getUserInfo(String uid) {
if (userCache.containsKey(uid)) {
return userCache.get(uid);
} else {
User user = ldapClient.getUser(uid);
userCache.put(uid, user);
return user;
}
}
}
And you want to verify if it will only call LdapClient one time even it is invoked for the same user multiple times. Then the test looks likes :
@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
@Mock
private LdapClient ldapClient;
@Test
public void testGetUserInfo() {
UserService sut = new UserService(ldapClient);
User u1 = sut.getUserInfo("foo");
User u2 = sut.getUserInfo("foo");
//verify ldapClient only call one time for the user "foo"
verify(ldapClient, times(1)).getUser("foo");
assertThat(u1).isEqualTo(u2);
}
}
For the actual LdapClient implementation that interact to the real LDAP server , you need to have another test for it which test with the embedded LDAP server that you are using.
Both tests together should give you a pretty good test coverage for the things that you want to verify. I just give you the idea but it should be easily applied to your actual situation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
