1 page.title=Testing Your Service 2 page.tags=testing, service 3 trainingnavtop=true 4 5 @jd:body 6 7 <!-- This is the training bar --> 8 <div id="tb-wrapper"> 9 <div id="tb"> 10 <h2>Dependencies and Prerequisites</h2> 11 12 <ul> 13 <li>Android 2.2 (API level 8) or higher</li> 14 <li><a href="{@docRoot}tools/testing-support-library/index.html"> 15 Android Testing Support Library</a></li> 16 <li><a href="{@docRoot}tools/studio/index.html">Android Studio 1.4.1 or higher</a>.</li> 17 </ul> 18 19 <h2>This lesson teaches you to</h2> 20 21 <ol> 22 <li><a href="#setup">Set Up Your Testing Environment</a></li> 23 <li><a href="#build">Create an Integrated Test for Services</a></li> 24 <li><a href="#run">Run Integration Tests for Services</a></li> 25 </ol> 26 27 <h2>You should also read</h2> 28 <ul> 29 <li><a href="{@docRoot}guide/components/services.html">Services</a></li> 30 </ul> 31 32 <h2>Try it out</h2> 33 34 <ul> 35 <li> 36 <a href="https://github.com/googlesamples/android-testing/tree/master/integration/ServiceTestRuleSample" 37 class="external-link">Service Test Code Samples</a></li> 38 </ul> 39 </div> 40 </div> 41 42 <p> 43 If you are implementing a local {@link android.app.Service} as a component of 44 your app, you should test the {@link android.app.Service} to ensure that it doesn't behave in an 45 unexpected way. You can create 46 <a href="{@docRoot}training/testing/unit-testing/instrumented-unit-tests.html"> 47 instrumented unit tests</a> to verify that the behavior in the {@link android.app.Service} 48 is correct; for example, the service stores and returns valid data values and performs 49 data operations correctly. 50 </p> 51 52 <p> 53 The <a href="{@docRoot}tools/testing-support-library/index.html">Android Testing Support Library</a> 54 provides an API for testing your {@link android.app.Service} objects in isolation. 55 The 56 <a href="{@docRoot}reference/android/support/test/rule/ServiceTestRule.html">ServiceTestRule</a> 57 class is a JUnit 4 rule that starts your service before your unit test methods 58 run, and shuts down the service after tests complete. By using this test rule, you ensure that the 59 connection to the service is always established before your test method runs. To 60 learn more about JUnit 4 rules, see the <a href="https://github.com/junit-team/junit/wiki/Rules" 61 class="external-link">JUnit documentation</a>. 62 </p> 63 64 <p style="note"> 65 <strong>Note</strong>: The 66 <a href="{@docRoot}reference/android/support/test/rule/ServiceTestRule.html">ServiceTestRule</a> 67 class does not support testing of {@link android.app.IntentService} objects. 68 If you need to test a {@link android.app.IntentService} object, you should encapsulate the logic 69 in a separate class and create a corresponding unit test instead. 70 </p> 71 72 <h2 id="setup">Set Up Your Testing Environment</h2> 73 <p>Before building your integration test for the service, make sure to configure your project for 74 instrumented tests, as described in 75 <a href="{@docRoot}training/testing/start/index.html#config-instrumented-tests"> 76 Getting Started with Testing</a>.</p> 77 78 <h2 id="build">Create an Integration Test for Services</h2> 79 <p>Your integration test should be written as a JUnit 4 test class. To learn more about creating 80 JUnit 4 test classes and using JUnit 4 assertion methods, see 81 <a href="{@docRoot}training/testing/unit-testing/instrumented-unit-tests.html#build"> 82 Create an Instrumented Unit Test Class</a>.</p> 83 84 <p>To create an integration test for your service, add the {@code @RunWith(AndroidJUnit4.class)} 85 annotation at the beginning of your test class definition. You also need to specify the 86 <a href="{@docRoot}reference/android/support/test/runner/AndroidJUnitRunner.html"> 87 {@code AndroidJUnitRunner}</a> class that the Android Testing Support Library provides as your 88 default test runner. This step is described in more detail in 89 <a href="{@docRoot}training/testing/unit-testing/instrumented-unit-tests.html#run"> 90 Run Instrumented Unit Tests</a>.</p> 91 92 <p>Next, create a 93 <a href="{@docRoot}reference/android/support/test/rule/ServiceTestRule.html">ServiceTestRule</a> 94 instance in your test by using the {@code @Rule} annotation.</p> 95 96 <pre> 97 @Rule 98 public final ServiceTestRule mServiceRule = new ServiceTestRule(); 99 </pre> 100 101 <p>The following example shows how you might implement an integration test for a service. 102 The test method {@code testWithBoundService} verifies that the app binds successfully to a 103 local service and that the service interface behaves correctly.</p> 104 105 <pre> 106 @Test 107 public void testWithBoundService() throws TimeoutException { 108 // Create the service Intent. 109 Intent serviceIntent = 110 new Intent(InstrumentationRegistry.getTargetContext(), 111 LocalService.class); 112 113 // Data can be passed to the service via the Intent. 114 serviceIntent.putExtra(LocalService.SEED_KEY, 42L); 115 116 // Bind the service and grab a reference to the binder. 117 IBinder binder = mServiceRule.bindService(serviceIntent); 118 119 // Get the reference to the service, or you can call 120 // public methods on the binder directly. 121 LocalService service = 122 ((LocalService.LocalBinder) binder).getService(); 123 124 // Verify that the service is working correctly. 125 assertThat(service.getRandomInt(), is(any(Integer.class))); 126 } 127 </pre> 128 129 <h2 id="run">Run Integration Tests for Services</h2> 130 <p> 131 You can run integration tests from <a href="{@docRoot}studio/index.html">Android Studio</a> or 132 from the command-line. Make sure to specify 133 <a href="{@docRoot}reference/android/support/test/runner/AndroidJUnitRunner.html"> 134 {@code AndroidJUnitRunner}</a> as the default instrumentation runner in your project. 135 </p> 136 <p> 137 To run the integration test for your service, follow the steps for running instrumented tests 138 described in <a href="{@docRoot}training/testing/start/index.html#run-instrumented-tests"> 139 Getting Started with Testing</a>. 140 </p> 141