Mock Client

Am working with Spring Boot, and one of my main methods is as below.

@Autowired private AerospikeClient asClient;

public String readOTPFromCache(String input) { Policy policy = new Policy(); JSONObject jsonObject = new JSONObject(input); Key readKey = new Key(nameserver, otp, jsonObject.getString(PHONENUMBER));

Record record = asClient.get(policy, readKey);
/* No OTP in cache for this phoneNumber */
if (record==null) {
    return null;
else {
    return new JSONObject(record.bins).toString();


How do I mock the client ? I have seen a couple of discussions on this forum, but couldn’t make out. Any help on this would be greatly appreciated.

Thanks in advance.

My test class is:

public class OTPTest { @InjectMocks OTP otp = new OTP();

@Mock AerospikeClient client;

public void testAerospike() {
    IAerospikeClient client = Mockito.mock(IAerospikeClient.class);
    Map <String, Object> map = new HashMap<>();
    map.put("key", "value");
    Record record = new Record(map, 0, -1);
    Mockito.when(client.get(any(Policy.class), any(Key.class))).thenReturn(record);


But in the main class, at the point where I am doing asClient.get(policy, readKey), it is throwing a null pointer exception.

Hi, You have two “client” variables. One inside testAerospike() and the other at the class level. Mockito.when is initializing the one inside testAerospike(). The class level one is not being initialized.

Sure, thanks. Will fix it

© 2015 Copyright Aerospike, Inc. | All rights reserved. Creators of the Aerospike Database.