Skip to content

Instantly share code, notes, and snippets.

@izmajlowiczl
Created May 9, 2014 08:37
Show Gist options
  • Save izmajlowiczl/072564cdbfb82340d7c2 to your computer and use it in GitHub Desktop.
Save izmajlowiczl/072564cdbfb82340d7c2 to your computer and use it in GitHub Desktop.
SharedPreferences with Robolectric
import android.content.SharedPreferences;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
public class TheLastSyncTimestamp {
private ILastSyncTimestamp tested;
@Before
public void beforeEachTest() {
final SharedPreferences syncPreferences = Robolectric.application.getSharedPreferences("_sync_preferences", 0);
tested = new LastSyncTimestamp(syncPreferences);
}
@Test
public void setLastSyncTimestamp_stores_timestamp() {
// given
tested.setLastSyncTimestamp(1);
// when
final long lastTimestampOrZero = tested.getLastTimestampOrZero();
// then
assertEquals(1, lastTimestampOrZero);
}
@Test
public void setLastSyncTimestamp_default_value() {
// when
final long lastTimestampOrZero = tested.getLastTimestampOrZero();
// then
assertEquals(0, lastTimestampOrZero);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment