Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

ActivityScenarioRule

public final class ActivityScenarioRule
extends ExternalResource

java.lang.Object
   ↳ org.junit.rules.ExternalResource
     ↳ androidx.test.ext.junit.rules.ActivityScenarioRule<A extends android.app.Activity>


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

You can access to scenario interface via getScenario() method. 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 ActivityTestRule. The previous version will be deprecated and eventually be removed from the library in the future.

Example:
  @RuleActivityScenarioRule<MyActivity> rule = new ActivityScenarioRule<>(MyActivity.class);

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

Summary

Public constructors

ActivityScenarioRule(Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

ActivityScenarioRule(Intent startActivityIntent)

Constructs ActivityScenarioRule for a given activity class and intent.

Public methods

ActivityScenario<A> getScenario()

Returns ActivityScenario of the given activity class.

Protected methods

void after()
void before()

Inherited methods

From class org.junit.rules.ExternalResource
From class java.lang.Object
From interface org.junit.rules.TestRule

Public constructors

ActivityScenarioRule

ActivityScenarioRule (Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

Parameters
activityClass Class: an activity class to launch

ActivityScenarioRule

ActivityScenarioRule (Intent startActivityIntent)

Constructs ActivityScenarioRule for a given activity class and intent.

Parameters
startActivityIntent Intent: an intent to start the activity

Public methods

getScenario

ActivityScenario<A> getScenario ()

Returns ActivityScenario of the given activity class.

Returns
ActivityScenario<A> a non-null ActivityScenario instance

Throws
NullPointerException if you call this method while test is not running

Protected methods

after

void after ()

before

void before ()

Throws
Throwable