InstrumentationRegistry


public final class InstrumentationRegistry


An exposed registry instance that holds a reference to the instrumentation running in the process and the instrumentation arguments. Provides an easy way for callers to get access to the instrumentation, application context, and instrumentation arguments bundle.

Summary

Public methods

static Bundle
@InlineMe(replacement = "androidx.test.platform.app.InstrumentationRegistry.getArguments()")
getArguments()

This method is deprecated.

Use androidx.test.platform.app.InstrumentationRegistry#getArguments().

static Context

This method is deprecated.

In most scenarios, androidx.test.core.app.ApplicationProvider#getApplicationContext() should be used instead of the instrumentation test context.

static Instrumentation
@InlineMe(replacement = "androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()")
getInstrumentation()

This method is deprecated.

Use androidx.test.platform.app.InstrumentationRegistry#getInstrumentation().

static Context

This method is deprecated.

Use androidx.test.core.app.ApplicationProvider#getApplicationContext().

static void
@InlineMe(replacement = "androidx.test.platform.app.InstrumentationRegistry.registerInstance(instrumentation," + " arguments)")
registerInstance(Instrumentation instrumentation, Bundle arguments)

This method is deprecated.

Use androidx.test.platform.app.InstrumentationRegistry#registerInstance(Instrumentation, Bundle).

Public methods

getArguments

@InlineMe(replacement = "androidx.test.platform.app.InstrumentationRegistry.getArguments()")
public static Bundle getArguments()

Returns a copy of the instrumentation arguments bundle. Use this method to get a Bundle containing the command-line arguments passed to Instrumentation into your test.

The bundle is not guaranteed to be present under all instrumentations.

Returns
Bundle

The arguments bundle for this instrumentation.

Throws
java.lang.IllegalStateException

If no argument bundle has been registered.

getContext

public static Context getContext()

Returns the context of this instrumentation's package. Use this method to get a Context representing Instrumentation#getContext() into your test.

Returns
Context

The instrumentation context.

getInstrumentation

@InlineMe(replacement = "androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()")
public static Instrumentation getInstrumentation()

Returns the instrumentation currently running. Use this method to get an Instrumentation into your test.

Returns
Instrumentation

The current instrumentation.

Throws
java.lang.IllegalStateException

If instrumentation hasn't been registered.

getTargetContext

public static Context getTargetContext()

Returns a context for the target application being instrumented. Use this method to get a Context representing Instrumentation#getTargetContext() into your test.

Returns
Context

The target application context.

registerInstance

@InlineMe(replacement = "androidx.test.platform.app.InstrumentationRegistry.registerInstance(instrumentation,"
              + " arguments)")
public static void registerInstance(Instrumentation instrumentation, Bundle arguments)

Records/exposes the instrumentation currently running and stores a copy of the instrumentation arguments bundle in the registry.

This is a global registry, so be aware of the impact of calling this method!

Parameters
Instrumentation instrumentation

The instrumentation currently running.

Bundle arguments

The arguments for this application. Null deregisters any existing arguments.