# Mock Client

**URL:** <https://discuss.aerospike.com/t/mock-client/7309>\
**Category:** General Discussion\
**Created:** [April 8, 2020, 1:56pm UTC](https://discuss.aerospike.com/t/mock-client/7309 "2020-04-08T13:56:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srinand\_Kaushik](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/srinand_kaushik/32/1150_2.png) [@Srinand\_Kaushik](https://discuss.aerospike.com/u/Srinand_Kaushik)\
**Post date:** [April 8, 2020, 1:56pm UTC](https://discuss.aerospike.com/t/mock-client/7309/1 "2020-04-08T13:56:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Srinand\_Kaushik](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/srinand_kaushik/32/1150_2.png) [@Srinand\_Kaushik](https://discuss.aerospike.com/u/Srinand_Kaushik)\
**Post date:** [April 9, 2020, 2:54am UTC](https://discuss.aerospike.com/t/mock-client/7309/2 "2020-04-09T02:54:32Z")

</div>

My test class is:

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

```
@Mock AerospikeClient client;

@Test
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);
    otp.readOTPFromCache("{\"phoneNumber\":\"xxx\"}");
}

```

}

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

---

<div class="post-metadata">

**Author:** ![jeffj](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@jeffj](https://discuss.aerospike.com/u/jeffj)\
**Post date:** [April 9, 2020, 4:08pm UTC](https://discuss.aerospike.com/t/mock-client/7309/3 "2020-04-09T16:08:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Srinand\_Kaushik](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/srinand_kaushik/32/1150_2.png) [@Srinand\_Kaushik](https://discuss.aerospike.com/u/Srinand_Kaushik)\
**Post date:** [April 10, 2020, 5:10am UTC](https://discuss.aerospike.com/t/mock-client/7309/4 "2020-04-10T05:10:31Z")

</div>

Sure, thanks. Will fix it
