UUIDGenerator { String getUUID() { String uuid = ""; synchronized (GENERATOR_LOCK) { uuid = getUUIDFromSharedPreferences(); if (uuid.isEmpty()) { uuid = generateAndSaveUUID(); } } return uuid; } UUIDGenerator(Context context);   }
@Test public void savesANonEmptyAndNotNullStringOnTheSharedPreferencesOnGettingIt() { UUIDGenerator generator = givenAGenerator(); String uuid = generator.getUUID(); assertNotNull(uuid); assertFalse(uuid.isEmpty()); }
@Test public void returnsTheGeneratedAndStoredUUIDAfterGettingIt() { UUIDGenerator generator = givenAGenerator(); generator.getUUID(); String storedUUID = getUUIDGeneratorSharedPreferences().getString(UUID_KEY, ""); assertEquals(storedUUID, generator.getUUID()); }
@Test public void returnsTheSameUUIDEvenIfITryToGetItTwice() { UUIDGenerator generator = givenAGenerator(); String firstUUIDCall = generator.getUUID(); String secondUUIDCall = generator.getUUID(); assertEquals(firstUUIDCall, secondUUIDCall); }
@Test public void returnsTheSameUUIDEvenIfTheGeneratorInstancesAreDifferent() { UUIDGenerator generator1 = givenAGenerator(); UUIDGenerator generator2 = givenAGenerator(); String firstUUIDCall = generator1.getUUID(); String secondUUIDCall = generator2.getUUID(); assertEquals(firstUUIDCall, secondUUIDCall); }