Skip to content

Most visited

Recently visited

navigation

ApplicationTestCase

public abstract class ApplicationTestCase
extends AndroidTestCase

java.lang.Object
   ↳ junit.framework.Assert
     ↳ junit.framework.TestCase
       ↳ android.test.AndroidTestCase
         ↳ android.test.ApplicationTestCase<T extends android.app.Application>


This class was deprecated in API level 24.
Use InstrumentationRegistry instead. New tests should be written using the Android Testing Support Library.

This test case provides a framework in which you can test Application classes in a controlled environment. It provides basic support for the lifecycle of a Application, and hooks by which you can inject various dependencies and control the environment in which your Application is tested.

Lifecycle Support. Every Application is designed to be accessed within a specific sequence of method calls (see Application for more details). In order to support the lifecycle of a Application, this test case will make the following calls at the following times.

  • The test case will not call onCreate() until your test calls createApplication(). This gives you a chance to set up or adjust any additional framework or test logic before onCreate().
  • After your test completes, the test case tearDown() method is automatically called, and it will stop & destroy your application by calling its onDestroy() method.

Dependency Injection. Every Application has one inherent dependency, the Context in which it runs. This framework allows you to inject a modified, mock, or isolated replacement for this dependencies, and thus perform a true unit test.

If simply run your tests as-is, your Application will be injected with a fully-functional Context. You can create and inject alternative types of Contexts by calling setContext(). You must do this before calling createApplication(). The test framework provides a number of alternatives for Context, including MockContext, RenamingDelegatingContext, and ContextWrapper.

Summary

Inherited fields

From class android.test.AndroidTestCase

Public constructors

ApplicationTestCase(Class<T> applicationClass)

Public methods

T getApplication()
Context getSystemContext()

Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Application under test.

final void testApplicationTestCaseSetUpProperly()

This test simply confirms that the Application class can be instantiated properly.

Protected methods

final void createApplication()

Start the Application under test, in the same way as if it was started by the system.

void setUp()

This will do the work to instantiate the Application under test.

void tearDown()

Shuts down the Application under test.

final void terminateApplication()

This will make the necessary calls to terminate the Application under test (it will call onTerminate().

Inherited methods

From class android.test.AndroidTestCase
From class junit.framework.TestCase
From class junit.framework.Assert
From class java.lang.Object
From interface junit.framework.Test

Public constructors

ApplicationTestCase

Added in API level 1
ApplicationTestCase (Class<T> applicationClass)

Parameters
applicationClass Class

Public methods

getApplication

Added in API level 1
T getApplication ()

Returns
T Returns the actual Application under test.

getSystemContext

Added in API level 1
Context getSystemContext ()

Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Application under test.

Returns
Context Returns a reference to a normal Context.

testApplicationTestCaseSetUpProperly

Added in API level 1
void testApplicationTestCaseSetUpProperly ()

This test simply confirms that the Application class can be instantiated properly.

Throws
Exception

Protected methods

createApplication

Added in API level 1
void createApplication ()

Start the Application under test, in the same way as if it was started by the system. If you use this method to start the Application, it will automatically be stopped by tearDown(). If you wish to inject a specialized Context for your test, by calling setContext(), you must do so before calling this method.

setUp

Added in API level 1
void setUp ()

This will do the work to instantiate the Application under test. After this, your test code must also start and stop the Application.

Throws
Exception

tearDown

Added in API level 1
void tearDown ()

Shuts down the Application under test. Also makes sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.

Throws
Exception

terminateApplication

Added in API level 1
void terminateApplication ()

This will make the necessary calls to terminate the Application under test (it will call onTerminate(). Ordinarily this will be called automatically (by tearDown(), but you can call it directly from your test in order to check for proper shutdown behaviors.

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.