Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A JUnit @Rule which launches an activity when your test starts. Stop extending gross ActivityInstrumentationBarfCase2!
Owner

JakeWharton commented Jan 9, 2015

Use:

@RunWith(AndroidJUnit4.class)
public final class DummyTest {
  @Rule public final ActivityRule<MainActivity> main = new ActivityRule<>(MainActivity.class);

  @Test public void things() {
    SystemClock.sleep(TimeUnit.SECONDS.toMillis(2));
  }
}

This is wonderful. Have you done anything similar for Android Unit Tests? I'd love to stop extending ActivityUnitTestCase

This is definitely non-gross. Thanks Jake.

QZenn commented Jun 18, 2015

Does anybody know how to get Instrumentation from android/support/test/rule/ActivityTestRule ?
I'm need it for Robotium instance.

Still use Jake's @rule for now...

Hi Jake,
Is there any means by which i can get the reference of Activity before its onCreate is called?
I want this because i want to inject mocks from tests into Activity.

Android testing support library already includes their ActivityTestRule. Why do we need this ActivityRule?

Owner

JakeWharton commented Aug 4, 2015

@IgorGanapolsky Mine came first.

hi all, need your expertise here. I m not sure why my JUnit4.class is in red color. I already sync gradle

/* JUnit4 & Espresso */
androidTestCompile 'com.android.support.test🏃0.3'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.3'

but not working?any 1 know why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment