ActivityScenarioRule


public final class ActivityScenarioRule<A extends Activity> extends ExternalResource


ActivityScenarioRule launches a given activity before the test starts and closes after the test.

You can access the androidx.test.core.app.ActivityScenario instance via getScenario. You may finish your activity manually in your test, it will not cause any problems and this rule does nothing after the test in such cases.

This rule is an upgraded version of the now deprecated .

This rule does not support calling scenario.getResult(). Use ActivityScenario.launchActivityForResult() when the Activity result is needed.

Example:

  @Rule
  public ActivityScenarioRule<MyActivity> rule =
      new ActivityScenarioRule<>(MyActivity.class);

  @Test
  public void myTest() {
    ActivityScenario<MyActivity> scenario = rule.getScenario();
    // Your test code goes here.
  }

Summary

Public fields

@Nullable ActivityScenario<A>

Public constructors

ActivityScenarioRule(Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

ActivityScenarioRule(Intent startActivityIntent)

Constructs ActivityScenarioRule with a given intent.

ActivityScenarioRule(
    Class<A> activityClass,
    @Nullable Bundle activityOptions
)
ActivityScenarioRule(
    Intent startActivityIntent,
    @Nullable Bundle activityOptions
)

Public methods

ActivityScenario<A>

Returns ActivityScenario of the given activity class.

Protected methods

void
void

Inherited methods

From org.junit.rules.ExternalResource
Statement
apply(Statement base, Description description)

Public fields

scenario

public @Nullable ActivityScenario<A> scenario

Public constructors

ActivityScenarioRule

public ActivityScenarioRule(Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

Parameters
Class<A> activityClass

an activity class to launch

ActivityScenarioRule

public ActivityScenarioRule(Intent startActivityIntent)

Constructs ActivityScenarioRule with a given intent.

Parameters
Intent startActivityIntent

an intent to start an activity

ActivityScenarioRule

public ActivityScenarioRule(
    Class<A> activityClass,
    @Nullable Bundle activityOptions
)
Parameters
Class<A> activityClass

an activity class to launch

@Nullable Bundle activityOptions

an activity options bundle to be passed along with the intent to start activity.

ActivityScenarioRule

public ActivityScenarioRule(
    Intent startActivityIntent,
    @Nullable Bundle activityOptions
)
Parameters
Intent startActivityIntent

an intent to start an activity

@Nullable Bundle activityOptions

an activity options bundle to be passed along with the intent to start activity.

Public methods

getScenario

public ActivityScenario<A> getScenario()

Returns ActivityScenario of the given activity class.

Returns
ActivityScenario<A>

a non-null ActivityScenario instance

Throws
java.lang.NullPointerException

if you call this method while test is not running

Protected methods

after

protected void after()

before

protected void before()