java.lang.Object | ||||
↳ | junit.framework.Assert | |||
↳ | junit.framework.TestCase | |||
↳ | android.test.AndroidTestCase | |||
↳ | android.test.ServiceTestCase<T extends android.app.Service> |
This test case provides a framework in which you can test Service classes in a controlled environment. It provides basic support for the lifecycle of a Service, and hooks by which you can inject various dependencies and control the environment in which your Service is tested.
Lifecycle Support.
Every Service is designed to be accessed within a specific sequence of
calls. Dependency Injection.
Every service has two inherent dependencies, the If simply run your tests as-is, your Service will be injected with a fully-functional
Context, and a generic startService(Intent)
or bindService(Intent)
. This gives you a chance
to set up or adjust any additional framework or test logic before
onCreate().startService(Intent)
or bindService(Intent)
the test case will call onCreate(), and then call the corresponding entry point in your service.
It will record any parameters or other support values necessary to support the lifecycle.tearDown()
function is
automatically called, and it will stop and destroy your service with the appropriate
calls (depending on how your test invoked the service.)Context
in
which it runs, and the Application
with which it is associated.
This framework allows you to inject modified, mock, or isolated replacements for these
dependencies, and thus perform a true unit test.
MockApplication
object.
You can create and inject alternatives to either of these by calling
setContext()
or
setApplication()
. You must do this before calling
startService() or bindService(). The test framework provides a
number of alternatives for Context, including {link android.test.mock.MockContext MockContext},
RenamingDelegatingContext
, and
ContextWrapper
.
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.test.AndroidTestCase
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Return the Application object being used by the Service under test.
| |||||||||||
Return a real (not mocked or instrumented) system Context that can be used when generating
Mock or other Context objects for your Service under test.
| |||||||||||
Set the application for use during the test.
| |||||||||||
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Start the service under test, in the same way as if it was started by
Context.bindService() , providing the
arguments it supplied. | |||||||||||
This will do the work to instantiate the Service under test.
| |||||||||||
Create the service under test and attach all injected dependencies (Context, Application) to
it.
| |||||||||||
This will make the necessary calls to stop (or unbind) the Service under test, and
call onDestroy().
| |||||||||||
Start the service under test, in the same way as if it was started by
Context.startService() , providing the
arguments it supplied. | |||||||||||
Shuts down the Service under test.
|
[Expand]
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
|
Return the Application object being used by the Service under test.
Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Service under test.
Set the application for use during the test. If your test does not call this function,
a new MockApplication
object will be generated.
application | The Application object that will be injected into the Service under test. |
---|
Start the service under test, in the same way as if it was started by
Context.bindService()
, providing the
arguments it supplied.
Return the communication channel to the service. May return null if
clients can not bind to the service. The returned
IBinder
is usually for a complex interface
that has been described using
aidl.
Note: In order to test with this interface, your service must implement a getService()
method, as shown in samples.ApiDemos.app.LocalService.
intent | The Intent as if supplied to bindService(Intent, ServiceConnection, int) . |
---|
This will do the work to instantiate the Service under test. After this, your test code must also start and stop the service.
Exception |
---|
Create the service under test and attach all injected dependencies (Context, Application) to
it. This will be called automatically by startService(Intent)
or by bindService(Intent)
.
If you wish to call setContext()
or
setApplication()
, you must do so before calling this function.
This will make the necessary calls to stop (or unbind) the Service under test, and
call onDestroy(). 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.
Start the service under test, in the same way as if it was started by
Context.startService()
, providing the
arguments it supplied. If you use this method to start the service, it will automatically
be stopped by tearDown()
.
intent | The Intent as if supplied to startService(Intent) .
|
---|
Shuts down the Service 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.
Exception |
---|