### Question:
SharedPrefsUniqueIdService implements UniqueIdService { @Override public Id getUniqueId(AnalyticsContext context) { if (context == null || context.getSystem() == null || context.getSystem().getPreferences() == null) { Log.d(TAG, "Unable to generate unique id, context has not been fully initialized"); return Id.getEmptyId(); } Id uniqueId = getIdFromPreferences(context.getSystem().getPreferences()); if (uniqueId == Id.getEmptyId()) { uniqueId = new Id(UUID.randomUUID().toString()); storeUniqueId(context.getSystem().getPreferences(), uniqueId); } return uniqueId; } SharedPrefsUniqueIdService(String appId, Context applicationContext); @Override Id getUniqueId(AnalyticsContext context);  }### Answer:
@Test public void getUniqueId_whenSharedPrefsIsNull_returnEmptyId() { when(mockSystem.getPreferences()).thenReturn(null); Id uniqueId = serviceToTest.getUniqueId(mockAnalyticsContext); assertThat(uniqueId.getValue(), is("")); }
@Test public void getUniqueId_whenIdDoesNotExist_createAndStoreId() { String expectedUniqueIdKey = "UniqueId"; when(mockPreferences.getString(eq(expectedUniqueIdKey), anyString())).thenReturn(null); Id uniqueId = serviceToTest.getUniqueId(mockAnalyticsContext); assertNotNull(uniqueId.getValue()); 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); Id uniqueId = serviceToTest.getUniqueId(mockAnalyticsContext); assertThat(uniqueId.getValue(), is(expectedUniqueId)); }