Annyce Davis

Davis Technology Consulting

  • Home
  • About Me
  • Blog
  • Courses
  • Newsletter

Unit Testing a Android Activity which contains a Runnable

November 16, 2010 by Annyce Davis

In order to unit test an Android Activity to verify that the Runnable is called you need to add a call to waitForIdle to the Instrumentation, which is called when all Runnables have been executed.

Here is the activity that we are testing:

public class SplashScreenActivity extends Activity {

public final int SPLASH_DISPLAY_LENGTH = 1200;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);

new Handler().postDelayed(new Runnable() {

public void run() {
Intent mainIntent = new Intent(SplashScreenActivity.this, NextActivity.class);
startActivity(mainIntent);
finish();
}

}, SPLASH_DISPLAY_LENGTH);
}
}

Here is the method in the Unit Test Class:

public void testSubLaunch() {
SplashScreenActivity mActivity = startActivity(new Intent(), null, null);
getInstrumentation().waitForIdle(new Runnable() {
public void run() {
assertNotNull(getStartedActivityIntent());
assertTrue(isFinishCalled());
}
});
}

Share this:

  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on Bluesky (Opens in new window) Bluesky
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Reddit (Opens in new window) Reddit

Related

Filed Under: Android Tagged With: Runnable, Unit Testing

Follow Me

  • Bluesky

Categories

  • Android (61)
  • Career (5)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • iOS (1)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (5)
  • Public Speaking (26)
  • Revenue (2)
  • RxJava (1)
  • Software Development (14)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Follow Me

  • Bluesky

Copyright © 2025 · All Rights Reserved · Log in