Skip to content

Instantly share code, notes, and snippets.

@kingargyle
Created August 20, 2014 18:11
Show Gist options
  • Save kingargyle/a48c6a91ee95d745ec2f to your computer and use it in GitHub Desktop.
Save kingargyle/a48c6a91ee95d745ec2f to your computer and use it in GitHub Desktop.
Verify that activity runOnUiThread is called using Robolectric and Mockito
/*
* Verifies that an activities runOnUiThread method is called and that
* appropriate class is called. In most cases we don't care about
* actually executing the runOnUiThread, just that it got called.
* By mocking out the Activity, we can use Mockito's verify method to
* make sure the method was called, with the expected Runnable class implementation
* passed into it.
*
* By doing this we avoid having to pause Robolectrics's UIScheduler and BackgroundSchedulers
* and then kick off the task. Since in this case it isn't actually necessary to run the code as it
* has it's own unit tests around it, and we don't want to starting getting into integration testing.
*/
@RunWith(RobolectricTestRunner.class)
@Config(emulateSdk = 18)
public class SerenityBackgroundLoadListenerTest {
View backgroundView;
Activity activity;
@Before
public void setUp() {
backgroundView = Mockito.mock(View.class);
activity = Mockito.mock(Activity.class);
doNothing().when(activity).runOnUiThread(
any(BackgroundBitmapDisplayer.class));
when(backgroundView.getContext()).thenReturn(activity);
}
@After
public void tearDown() {
}
@Test
public void assertThatBackgroundBitmapDisplayerIsRunOnUIThread() {
SerenityBackgroundLoaderListener listener = new SerenityBackgroundLoaderListener(
backgroundView, R.drawable.movies);
listener.onLoadingComplete(null, backgroundView, null);
verify(activity).runOnUiThread(any(BackgroundBitmapDisplayer.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment