### Question:
SharedPrefsUniqueIdService { public String getUniqueId(PinpointContext context) { if (context == null || context.getSystem() == null || context.getSystem().getPreferences() == null) { log.debug("Unable to generate unique id, pinpointContext has not been fully initialized."); return ""; } String uniqueId = getIdFromPreferences(context.getSystem() .getPreferences()); if (uniqueId == null || uniqueId == "") { uniqueId = UUID.randomUUID().toString(); storeUniqueId(context.getSystem().getPreferences(), uniqueId); } return uniqueId; } SharedPrefsUniqueIdService(String appId,
                                             Context applicationContext); String getUniqueId(PinpointContext context);  }### Answer:
@Test public void getUniqueId_whenSharedPrefsIsNull_returnEmptyId() { when(mockSystem.getPreferences()).thenReturn(null); String uniqueId = serviceToTest.getUniqueId(mockPinpointContext); assertThat(uniqueId, is("")); }
@Test public void getUniqueId_whenIdDoesNotExist_createAndStoreId() { String expectedUniqueIdKey = "UniqueId"; when(mockPreferences.getString(eq(expectedUniqueIdKey), anyString())) .thenReturn(null); String uniqueId = serviceToTest.getUniqueId(mockPinpointContext); assertNotNull(uniqueId); verify(mockPreferences, times(1)) .putString(eq(expectedUniqueIdKey), any(String.class)); }
@Test public void getUniqueId_whenIdExist_recallStoredId() { String expectedUniqueIdKey = "UniqueId"; String expectedUniqueId = "98765"; when(mockPreferences.getString(eq(expectedUniqueIdKey), anyString())) .thenReturn( expectedUniqueId); String uniqueId = serviceToTest.getUniqueId(mockPinpointContext); assertThat(uniqueId, is(expectedUniqueId)); }