Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level
24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Fields](https://developer.android.com/reference/android/test/ServiceTestCase#inhfields) \| [Ctors](https://developer.android.com/reference/android/test/ServiceTestCase#pubctors) \| [Methods](https://developer.android.com/reference/android/test/ServiceTestCase#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/test/ServiceTestCase#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/test/ServiceTestCase#inhmethods)

# ServiceTestCase

*** ** * ** ***


`
public


abstract
class
ServiceTestCase
`


`


`

`

extends https://developer.android.com/reference/android/test/AndroidTestCase


`

`


`

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [android.test.AndroidTestCase](https://developer.android.com/reference/android/test/AndroidTestCase) ||
|   | ↳ | android.test.ServiceTestCase\<T extends [android.app.Service](https://developer.android.com/reference/android/app/Service)\> |

<br />

*** ** * ** ***


**This class was deprecated
in API level 24.**   

Use
[ServiceTestRule](https://developer.android.com/reference/android/support/test/rule/ServiceTestRule) instead. New tests should be written using the
[Android Testing Support Library](https://developer.android.com/tools/testing-support-library).

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 with which you can inject various dependencies and control
the environment in which your Service is tested.

### Developer Guides

For more information about application testing, read the
[Testing](https://developer.android.com/guide/topics/testing) developer guide.

**Lifecycle Support.**
A Service is accessed with a specific sequence of
calls, as described in the
[Services](http://developer.android.com/guide/topics/fundamentals/services.html)
document. In order to support the lifecycle of a Service,
`ServiceTestCase` enforces this protocol:

- The `https://developer.android.com/reference/android/test/ServiceTestCase#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.
- The test case waits to call `https://developer.android.com/reference/android/app/Service#onCreate()` until one of your test methods calls `https://developer.android.com/reference/android/test/ServiceTestCase#startService(android.content.Intent)` or `https://developer.android.com/reference/android/test/ServiceTestCase#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.
- When one of your test methods calls `https://developer.android.com/reference/android/test/ServiceTestCase#startService(android.content.Intent)` or `https://developer.android.com/reference/android/test/ServiceTestCase#bindService(android.content.Intent)`, the test case calls `https://developer.android.com/reference/android/app/Service#onCreate()` and then calls either `https://developer.android.com/reference/android/content/ContextWrapper#startService(android.content.Intent)` or `https://developer.android.com/reference/android/content/ContextWrapper#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)`, as appropriate. It also stores values needed to track and support the lifecycle.
- After each test method finishes, the test case calls the `https://developer.android.com/reference/android/test/ServiceTestCase#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 `https://developer.android.com/reference/android/content/Context` and its
associated `https://developer.android.com/reference/android/app/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
`https://developer.android.com/reference/android/test/mock/MockApplication` object. You can inject
alternatives to either of these by invoking
`https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context)` or
`https://developer.android.com/reference/android/test/ServiceTestCase#setApplication(android.app.Application)`. You must do this *before* calling
startService() or bindService(). The test framework provides a
number of alternatives for Context, including
`https://developer.android.com/reference/android/test/mock/MockContext`,
`https://developer.android.com/reference/android/test/RenamingDelegatingContext`,
`https://developer.android.com/reference/android/content/ContextWrapper`, and
`https://developer.android.com/reference/android/test/IsolatedContext`.

## Summary

| ### Inherited fields |
|---|
| From class `https://developer.android.com/reference/android/test/AndroidTestCase` |---|---| | ` protected https://developer.android.com/reference/android/content/Context` | `https://developer.android.com/reference/android/test/AndroidTestCase#mContext` | |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/test/ServiceTestCase#ServiceTestCase(java.lang.Class<T>)(https://developer.android.com/reference/java/lang/Class<T> serviceClass) ` Constructor |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/app/Application` | ` https://developer.android.com/reference/android/test/ServiceTestCase#getApplication()() ` Returns the Application object in use by the service under test. |
| ` T` | ` https://developer.android.com/reference/android/test/ServiceTestCase#getService()() ` |
| ` https://developer.android.com/reference/android/content/Context` | ` https://developer.android.com/reference/android/test/ServiceTestCase#getSystemContext()() ` Returns the real system context that is saved by `https://developer.android.com/reference/android/test/ServiceTestCase#setUp()`. |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#setApplication(android.app.Application)(https://developer.android.com/reference/android/app/Application application) ` Sets the application that is used during the test. |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#testServiceTestCaseSetUpProperly()() ` Tests that `https://developer.android.com/reference/android/test/ServiceTestCase#setupService()` runs correctly and issues an `https://developer.android.com/reference/junit/framework/Assert#assertNotNull(java.lang.String,%20java.lang.Object)` if it does. |

| ### Protected methods ||
|---|---|
| ` https://developer.android.com/reference/android/os/IBinder` | ` https://developer.android.com/reference/android/test/ServiceTestCase#bindService(android.content.Intent)(https://developer.android.com/reference/android/content/Intent intent) ` <br /> Starts the service under test, in the same way as if it were started by `https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)` with an `https://developer.android.com/reference/android/content/Intent` that identifies a service. |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#setUp()() ` Gets the current system context and stores it. |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#setupService()() ` Creates the service under test and attaches all injected dependencies (Context, Application) to it. |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#shutdownService()() ` Makes the necessary calls to stop (or unbind) the service under test, and calls onDestroy(). |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#startService(android.content.Intent)(https://developer.android.com/reference/android/content/Intent intent) ` Starts the service under test, in the same way as if it were started by `https://developer.android.com/reference/android/content/Context#startService(android.content.Intent)` with an `https://developer.android.com/reference/android/content/Intent` that identifies a service. |
| ` void` | ` https://developer.android.com/reference/android/test/ServiceTestCase#tearDown()() ` <br /> Shuts down the service under test. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/android/test/AndroidTestCase ` |---|---| | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#assertActivityRequiresPermission(java.lang.String,%20java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/lang/String className, https://developer.android.com/reference/java/lang/String permission) ` Asserts that launching a given activity is protected by a particular permission by attempting to start the activity and validating that a `https://developer.android.com/reference/java/lang/SecurityException` is thrown that mentions the permission in its error message. | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#assertReadingContentUriRequiresPermission(android.net.Uri,%20java.lang.String)(https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/java/lang/String permission) ` Asserts that reading from the content uri requires a particular permission by querying the uri and ensuring a `https://developer.android.com/reference/java/lang/SecurityException` is thrown mentioning the particular permission. | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#assertWritingContentUriRequiresPermission(android.net.Uri,%20java.lang.String)(https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/java/lang/String permission) ` Asserts that writing to the content uri requires a particular permission by inserting into the uri and ensuring a `https://developer.android.com/reference/java/lang/SecurityException` is thrown mentioning the particular permission. | | ` https://developer.android.com/reference/android/content/Context` | ` https://developer.android.com/reference/android/test/AndroidTestCase#getContext()() ` | | ` https://developer.android.com/reference/android/content/Context` | ` https://developer.android.com/reference/android/test/AndroidTestCase#getTestContext()() ` Returns the test context that was set via `https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context)`. | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#scrubClass(java.lang.Class<?>)(https://developer.android.com/reference/java/lang/Class<?> testCaseClass) ` This function is called by various TestCase implementations, at tearDown() time, in order to scrub out any class variables. | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context)(https://developer.android.com/reference/android/content/Context context) ` | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context)(https://developer.android.com/reference/android/content/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` | ` https://developer.android.com/reference/android/test/AndroidTestCase#setUp()() ` | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#tearDown()() ` | | ` void` | ` https://developer.android.com/reference/android/test/AndroidTestCase#testAndroidTestCaseSetupProperly()() ` | ||
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### ServiceTestCase

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ServiceTestCase (Class<T> serviceClass)
```

Constructor

<br />

| Parameters ||
|---|---|
| `serviceClass` | `Class`: The type of the service under test. <br /> |

## Public methods

### getApplication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Application getApplication ()
```

Returns the Application object in use by the service under test.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/app/Application` | The application object. <br /> |

**See also:**

- `https://developer.android.com/reference/android/test/ServiceTestCase#setApplication(android.app.Application)`

### getService

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public T getService ()
```

<br />

<br />

| Returns ||
|---|---|
| `T` | An instance of the service under test. This instance is created automatically when a test calls `https://developer.android.com/reference/android/test/ServiceTestCase#startService(android.content.Intent)` or `https://developer.android.com/reference/android/test/ServiceTestCase#bindService(android.content.Intent)`. <br /> |

### getSystemContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Context getSystemContext ()
```

Returns the real system context that is saved by `https://developer.android.com/reference/android/test/ServiceTestCase#setUp()`. Use it to create
mock or other types of context objects for the service under test.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/Context` | A normal system context. <br /> |

### setApplication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setApplication (Application application)
```

Sets the application that is used during the test. If you do not call this method,
a new `https://developer.android.com/reference/android/test/mock/MockApplication` object is used.

<br />

| Parameters ||
|---|---|
| `application` | `Application`: The Application object that is used by the service under test. <br /> |

**See also:**

- `https://developer.android.com/reference/android/test/ServiceTestCase#getApplication()`

### testServiceTestCaseSetUpProperly

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void testServiceTestCaseSetUpProperly ()
```

Tests that `https://developer.android.com/reference/android/test/ServiceTestCase#setupService()` runs correctly and issues an
`https://developer.android.com/reference/junit/framework/Assert#assertNotNull(java.lang.String,%20java.lang.Object)` if it does.
You can override this test method if you wish.

<br />

| Throws ||
|---|---|
|   | java.lang.Exception |
| `https://developer.android.com/reference/java/lang/Exception` |   |

## Protected methods

### bindService

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected IBinder bindService (Intent intent)
```

<br />


Starts the service under test, in the same way as if it were started by
`https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)` with an
`https://developer.android.com/reference/android/content/Intent` that identifies a service.


Notice that the parameters are different. You do not provide a
`https://developer.android.com/reference/android/content/ServiceConnection` object or the flags parameter. Instead,
you only provide the Intent. The method returns an object whose type is a
subclass of `https://developer.android.com/reference/android/os/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 `https://developer.android.com/reference/android/content/Context#BIND_AUTO_CREATE`.


See [Designing a Remote Interface
Using AIDL](https://developer.android.com/guide/components/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.

<br />

<br />

| Parameters ||
|---|---|
| `intent` | `Intent`: An Intent object of the form expected by `https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.Context.BindServiceFlags,%20java.util.concurrent.Executor,%20android.content.ServiceConnection)`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/IBinder` | An object whose type is a subclass of IBinder, for making further calls into the service. <br /> |

### setUp

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void setUp ()
```

Gets the current system context and stores it.

Extend this method to do your own test initialization. If you do so, you
must call `super.setUp()` as the first statement in your override. The method is
called before each test method is executed.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Exception` |   |

### setupService

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void setupService ()
```

Creates the service under test and attaches all injected dependencies
(Context, Application) to it. This is called automatically by `https://developer.android.com/reference/android/test/ServiceTestCase#startService(android.content.Intent)` or
by `https://developer.android.com/reference/android/test/ServiceTestCase#bindService(android.content.Intent)`.
If you need to call `https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context)` or
`https://developer.android.com/reference/android/test/ServiceTestCase#setApplication(android.app.Application)`, do so before calling this method.

<br />

### shutdownService

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void shutdownService ()
```

Makes the necessary calls to stop (or unbind) the service under test, and
calls onDestroy(). Ordinarily this is called automatically (by `https://developer.android.com/reference/android/test/ServiceTestCase#tearDown()`, but
you can call it directly from your test in order to check for proper shutdown behavior.

<br />

### startService

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void startService (Intent intent)
```

Starts the service under test, in the same way as if it were started by
`https://developer.android.com/reference/android/content/Context#startService(android.content.Intent)` with
an `https://developer.android.com/reference/android/content/Intent` that identifies a service.
If you use this method to start the service, it is automatically stopped by
`https://developer.android.com/reference/android/test/ServiceTestCase#tearDown()`.

<br />

| Parameters ||
|---|---|
| `intent` | `Intent`: An Intent that identifies a service, of the same form as the Intent passed to `https://developer.android.com/reference/android/content/Context#startService(android.content.Intent)`. <br /> |

### tearDown

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void tearDown ()
```

<br />


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.

<br />

<br />

| Throws ||
|---|---|
|   | java.lang.Exception |
| `https://developer.android.com/reference/java/lang/Exception` |   |