ApplicationTestCase
public
abstract
class
ApplicationTestCase
extends AndroidTestCase
This class is deprecated.
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
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
void
|
assertActivityRequiresPermission(String packageName, String className, String permission)
Asserts that launching a given activity is protected by a particular permission by
attempting to start the activity and validating that a SecurityException
is thrown that mentions the permission in its error message.
|
void
|
assertReadingContentUriRequiresPermission(Uri uri, String permission)
Asserts that reading from the content uri requires a particular permission by querying the
uri and ensuring a SecurityException is thrown mentioning the particular permission.
|
void
|
assertWritingContentUriRequiresPermission(Uri uri, String permission)
Asserts that writing to the content uri requires a particular permission by inserting into
the uri and ensuring a SecurityException is thrown mentioning the particular
permission.
|
Context
|
getContext()
|
Context
|
getTestContext()
Returns the test context that was set via setTestContext(android.content.Context) .
|
void
|
scrubClass(Class<?> testCaseClass)
This function is called by various TestCase implementations, at tearDown() time, in order
to scrub out any class variables.
|
void
|
setContext(Context context)
|
void
|
setTestContext(Context context)
Test context can be used to access resources from the test's own package
as opposed to the resources from the test target package.
|
void
|
setUp()
Sets up the fixture, for example, open a network connection.
|
void
|
tearDown()
Tears down the fixture, for example, close a network connection.
|
void
|
testAndroidTestCaseSetupProperly()
|
|
From class
junit.framework.TestCase
int
|
countTestCases()
Counts the number of test cases executed by run(TestResult result).
|
TestResult
|
createResult()
Creates a default TestResult object
|
String
|
getName()
Gets the name of a TestCase
|
TestResult
|
run()
A convenience method to run this test, collecting the results with a
default TestResult object.
|
void
|
run(TestResult result)
Runs the test case and collects the results in TestResult.
|
void
|
runBare()
Runs the bare test sequence.
|
void
|
runTest()
Override to run the test and assert its state.
|
void
|
setName(String name)
Sets the name of a TestCase
|
void
|
setUp()
Sets up the fixture, for example, open a network connection.
|
void
|
tearDown()
Tears down the fixture, for example, close a network connection.
|
String
|
toString()
Returns a string representation of the test case
|
|
From class
junit.framework.Assert
static
void
|
assertEquals(boolean expected, boolean actual)
Asserts that two booleans are equal.
|
static
void
|
assertEquals(String message, long expected, long actual)
Asserts that two longs are equal.
|
static
void
|
assertEquals(short expected, short actual)
Asserts that two shorts are equal.
|
static
void
|
assertEquals(String message, String expected, String actual)
Asserts that two Strings are equal.
|
static
void
|
assertEquals(String message, int expected, int actual)
Asserts that two ints are equal.
|
static
void
|
assertEquals(Object expected, Object actual)
Asserts that two objects are equal.
|
static
void
|
assertEquals(String message, boolean expected, boolean actual)
Asserts that two booleans are equal.
|
static
void
|
assertEquals(String expected, String actual)
Asserts that two Strings are equal.
|
static
void
|
assertEquals(String message, short expected, short actual)
Asserts that two shorts are equal.
|
static
void
|
assertEquals(String message, Object expected, Object actual)
Asserts that two objects are equal.
|
static
void
|
assertEquals(char expected, char actual)
Asserts that two chars are equal.
|
static
void
|
assertEquals(byte expected, byte actual)
Asserts that two bytes are equal.
|
static
void
|
assertEquals(double expected, double actual, double delta)
Asserts that two doubles are equal concerning a delta.
|
static
void
|
assertEquals(String message, char expected, char actual)
Asserts that two chars are equal.
|
static
void
|
assertEquals(float expected, float actual, float delta)
Asserts that two floats are equal concerning a delta.
|
static
void
|
assertEquals(String message, double expected, double actual, double delta)
Asserts that two doubles are equal concerning a delta.
|
static
void
|
assertEquals(String message, byte expected, byte actual)
Asserts that two bytes are equal.
|
static
void
|
assertEquals(String message, float expected, float actual, float delta)
Asserts that two floats are equal concerning a positive delta.
|
static
void
|
assertEquals(long expected, long actual)
Asserts that two longs are equal.
|
static
void
|
assertEquals(int expected, int actual)
Asserts that two ints are equal.
|
static
void
|
assertFalse(String message, boolean condition)
Asserts that a condition is false.
|
static
void
|
assertFalse(boolean condition)
Asserts that a condition is false.
|
static
void
|
assertNotNull(Object object)
Asserts that an object isn't null.
|
static
void
|
assertNotNull(String message, Object object)
Asserts that an object isn't null.
|
static
void
|
assertNotSame(String message, Object expected, Object actual)
Asserts that two objects do not refer to the same object.
|
static
void
|
assertNotSame(Object expected, Object actual)
Asserts that two objects do not refer to the same object.
|
static
void
|
assertNull(String message, Object object)
Asserts that an object is null.
|
static
void
|
assertNull(Object object)
Asserts that an object is null.
|
static
void
|
assertSame(String message, Object expected, Object actual)
Asserts that two objects refer to the same object.
|
static
void
|
assertSame(Object expected, Object actual)
Asserts that two objects refer to the same object.
|
static
void
|
assertTrue(String message, boolean condition)
Asserts that a condition is true.
|
static
void
|
assertTrue(boolean condition)
Asserts that a condition is true.
|
static
void
|
fail(String message)
Fails a test with the given message.
|
static
void
|
fail()
Fails a test with no message.
|
static
void
|
failNotEquals(String message, Object expected, Object actual)
|
static
void
|
failNotSame(String message, Object expected, Object actual)
|
static
void
|
failSame(String message)
|
static
String
|
format(String message, Object expected, Object actual)
|
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait(long timeout, int nanos)
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.
|
final
void
|
wait(long timeout)
Causes the current thread to wait until either another thread invokes the
notify() method or the
notifyAll() method for this object, or a
specified amount of time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object.
|
|
|
Public constructors
ApplicationTestCase
public ApplicationTestCase (Class<T> applicationClass)
Parameters |
applicationClass |
Class |
Public methods
getApplication
public T getApplication ()
Returns |
T |
Returns the actual Application under test. |
getSystemContext
public 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
public final void testApplicationTestCaseSetUpProperly ()
This test simply confirms that the Application class can be instantiated properly.
Protected methods
createApplication
protected final 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
protected 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.
tearDown
protected 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.
terminateApplication
protected final 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.