@Deprecated public abstract class ServiceTestCase<T extends Service> extends AndroidTestCase
For more information about application testing, read the Testing developer guide.
Lifecycle Support.
A Service is accessed with a specific sequence of
calls, as described in the
Services
document. In order to support the lifecycle of a Service,
ServiceTestCase
enforces this protocol:
setUp()
method is called before each test method. The base implementation
gets the system context. If you override setUp()
, you must call
super.setUp()
as the first statement in your override.
Service.onCreate()
until one of your
test methods calls startService(android.content.Intent)
or bindService(android.content.Intent)
. This gives you an
opportunity to set up or adjust any additional framework or test logic before you test
the running service.
ServiceTestCase.startService()
or ServiceTestCase.bindService()
, the test case calls
Service.onCreate()
and then calls either
Service.startService(Intent)
or
Service.bindService(Intent, ServiceConnection, int)
, as appropriate. It also stores
values needed to track and support the lifecycle.
tearDown()
method. This
method stops and destroys the service with the appropriate calls, depending on how the
service was started. If you override tearDown()
, your must call the
super.tearDown()
as the last statement in your override.
Dependency Injection.
A service has two inherent dependencies, its Context
and its
associated Application
. The ServiceTestCase framework
allows you to inject modified, mock, or isolated replacements for these dependencies, and
thus perform unit tests with controlled dependencies in an isolated environment.
By default, the test case is injected with a full system context and a generic
MockApplication
object. You can inject
alternatives to either of these by invoking
setContext()
or
setApplication()
. You must do this before calling
startService() or bindService(). The test framework provides a
number of alternatives for Context, including
MockContext
,
RenamingDelegatingContext
,
ContextWrapper
, and
IsolatedContext
.
mContext
Constructor and Description |
---|
ServiceTestCase(Class<T> serviceClass)
Deprecated.
Constructor
|
Modifier and Type | Method and Description |
---|---|
protected IBinder |
bindService(Intent intent)
Deprecated.
Starts the service under test, in the same way as if it were started by
Context.bindService(Intent, ServiceConnection, flags) with an
Intent that identifies a service. |
Application |
getApplication()
Deprecated.
Returns the Application object in use by the service under test.
|
T |
getService()
Deprecated.
|
Context |
getSystemContext()
Deprecated.
Returns the real system context that is saved by
setUp() . |
void |
setApplication(Application application)
Deprecated.
Sets the application that is used during the test.
|
protected void |
setUp()
Deprecated.
Gets the current system context and stores it.
|
protected void |
setupService()
Deprecated.
Creates the service under test and attaches all injected dependencies
(Context, Application) to it.
|
protected void |
shutdownService()
Deprecated.
Makes the necessary calls to stop (or unbind) the service under test, and
calls onDestroy().
|
protected void |
startService(Intent intent)
Deprecated.
Starts the service under test, in the same way as if it were started by
Context.startService(Intent) with
an Intent that identifies a service. |
protected void |
tearDown()
Deprecated.
Shuts down the service under test.
|
void |
testServiceTestCaseSetUpProperly()
Deprecated.
Tests that
setupService() runs correctly and issues an
junit.framework.Assert#assertNotNull(String, Object) if it does. |
assertActivityRequiresPermission, assertReadingContentUriRequiresPermission, assertWritingContentUriRequiresPermission, getContext, getTestContext, scrubClass, setContext, setTestContext, testAndroidTestCaseSetupProperly
public T getService()
startService(android.content.Intent)
or bindService(android.content.Intent)
.protected void setUp() throws Exception
super.setUp()
as the first statement in your override. The method is
called before each test method is executed.setUp
in class AndroidTestCase
Exception
protected void setupService()
startService(android.content.Intent)
or
by bindService(android.content.Intent)
.
If you need to call setContext()
or
setApplication()
, do so before calling this method.protected void startService(Intent intent)
Context.startService(Intent)
with
an Intent
that identifies a service.
If you use this method to start the service, it is automatically stopped by
tearDown()
.intent
- An Intent that identifies a service, of the same form as the Intent passed to
Context.startService(Intent)
.protected IBinder bindService(Intent intent)
Starts the service under test, in the same way as if it were started by
Context.bindService(Intent, ServiceConnection, flags)
with an
Intent
that identifies a service.
Notice that the parameters are different. You do not provide a
ServiceConnection
object or the flags parameter. Instead,
you only provide the Intent. The method returns an object whose type is a
subclass of IBinder
, or null if the method fails. An IBinder
object refers to a communication channel between the application and
the service. The flag is assumed to be Context.BIND_AUTO_CREATE
.
See Designing a Remote Interface Using AIDL for more information about the communication channel object returned by this method.
Note: To be able to use bindService in a test, the service must implement getService() method. An example of this is in the ApiDemos sample application, in the LocalService demo.intent
- An Intent object of the form expected by
Context.bindService(android.content.Intent, android.content.ServiceConnection, int)
.protected void shutdownService()
tearDown()
, but
you can call it directly from your test in order to check for proper shutdown behavior.protected void tearDown() throws Exception
Shuts down the service under test. Ensures all resources are cleaned up and garbage collected before moving on to the next test. This method is called after each test method.
Subclasses that override this method must call super.tearDown()
as their
last statement.
tearDown
in class AndroidTestCase
Exception
public void setApplication(Application application)
MockApplication
object is used.application
- The Application object that is used by the service under test.getApplication()
public Application getApplication()
setApplication(android.app.Application)
public Context getSystemContext()
setUp()
. Use it to create
mock or other types of context objects for the service under test.public void testServiceTestCaseSetUpProperly() throws Exception
setupService()
runs correctly and issues an
junit.framework.Assert#assertNotNull(String, Object)
if it does.
You can override this test method if you wish.Exception