Create a gist now

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton 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));
  }
}
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));
  }
}
@drewhannay

This comment has been minimized.

Show comment
Hide comment
@drewhannay

drewhannay Mar 10, 2015

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

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

@christopherperry

This comment has been minimized.

Show comment
Hide comment
@christopherperry

christopherperry Mar 19, 2015

This is definitely non-gross. Thanks Jake.

This is definitely non-gross. Thanks Jake.

@JakeWharton

This comment has been minimized.

Show comment
Hide comment
@QZenn

This comment has been minimized.

Show comment
Hide comment
@QZenn

QZenn 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...

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...

@RajeshBatth

This comment has been minimized.

Show comment
Hide comment
@RajeshBatth

RajeshBatth Jul 13, 2015

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.

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.

@IgorGanapolsky

This comment has been minimized.

Show comment
Hide comment
@IgorGanapolsky

IgorGanapolsky Jul 24, 2015

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

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

@JakeWharton

This comment has been minimized.

Show comment
Hide comment
Owner

JakeWharton commented Aug 4, 2015

@IgorGanapolsky Mine came first.

@stefanlam88

This comment has been minimized.

Show comment
Hide comment
@stefanlam88

stefanlam88 Aug 25, 2015

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?

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