public final class

ActivityScenarioRule

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

Class Overview

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

You can access the 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 ERROR(/androidx.test.rule.ActivityTestRule).

Example:

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

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

Summary

Public Constructors
ActivityScenarioRule(Class<A> activityClass)
Constructs ActivityScenarioRule for a given activity class.
ActivityScenarioRule(Class<A> activityClass, Bundle activityOptions)
ActivityScenarioRule(Intent startActivityIntent)
Constructs ActivityScenarioRule with a given intent.
ActivityScenarioRule(Intent startActivityIntent, Bundle activityOptions)
Public Methods
ActivityScenario<A> getScenario()
Returns ActivityScenario of the given activity class.
Protected Methods
void after()
void before()
[Expand]
Inherited Methods
From class org.junit.rules.ExternalResource
From class java.lang.Object
From interface org.junit.rules.TestRule

Public Constructors

public ActivityScenarioRule (Class<A> activityClass)

Constructs ActivityScenarioRule for a given activity class.

Parameters
activityClass Class: an activity class to launch

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

Parameters
activityClass Class
activityOptions Bundle: an activity options bundle to be passed along with the intent to start activity.

public ActivityScenarioRule (Intent startActivityIntent)

Constructs ActivityScenarioRule with a given intent.

Parameters
startActivityIntent Intent: an intent to start an activity

public ActivityScenarioRule (Intent startActivityIntent, Bundle activityOptions)

Parameters
startActivityIntent Intent
activityOptions Bundle: an activity options bundle to be passed along with the intent to start activity.

Public Methods

public 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

protected void after ()

protected void before ()

Throws
Throwable