1page.title=Creating Unit Tests 2trainingnavtop=true 3@jd:body 4 5<!-- This is the training bar --> 6<div id="tb-wrapper"> 7<div id="tb"> 8 9<h2>This lesson teaches you to</h2> 10<ol> 11 <li><a href="#testcase">Create a Test Case for Activity Unit Testing</a> 12 <li><a href="#test_method">Validate Launch of Another Activity</a> 13</ol> 14 15<h2>Try it out</h2> 16<div class="download-box"> 17 <a href="http://developer.android.com/shareables/training/AndroidTestingFun.zip" 18class="button">Download the demo</a> 19 <p class="filename">AndroidTestingFun.zip</p> 20</div> 21 22</div> 23</div> 24 25<p>An {@link android.app.Activity} unit test is an excellent way to quickly 26verify the state of an {@link android.app.Activity} and its interactions with 27other components in isolation (that is, disconnected from the rest of the 28system). A unit test generally tests the smallest possible unit of code 29(which could be a method, class, or component), without dependencies on system 30or network resources. For example, you can write a unit test to check 31that an {@link android.app.Activity} has the correct layout or that it 32triggers an {@link android.content.Intent} object correctly.</p> 33<p>Unit tests are generally not suitable for testing complex UI interaction 34events with the system. Instead, you should use 35the {@link android.test.ActivityInstrumentationTestCase2} class, as described 36in <a href="activity-ui-testing.html">Testing UI Components</a>.</p> 37<p>This lesson shows how you can write a unit test to verify that an 38{@link android.content.Intent} is triggered to launch another 39{@link android.app.Activity}. 40Since the test runs in an isolated environment, the 41{@link android.content.Intent} 42is not actually sent to the Android system, but you can inspect that the 43{@link android.content.Intent} object's payload data is accurate.</p> 44<p>For a complete test case example, take a look at 45{@code LaunchActivityTest.java} in the sample app.</p> 46 47<p class="note"><strong>Note: </strong>To test against system or external 48dependencies, you can use mock objects from a mocking 49framework and inject them into your unit tests. To learn more about the mocking 50framework provided by Android, see 51<a href="{@docRoot}tools/testing/testing_android.html#MockObjectClasses}">Mock 52Object Classes</a>.</p> 53 54<h2 id="testcase">Create a Test Case for Activity Unit Testing</h2> 55<p>The {@link android.test.ActivityUnitTestCase} class provides support for 56isolated testing of a single {@link android.app.Activity}. To create a unit 57test for your {@link android.app.Activity}, your test class should extend 58{@link android.test.ActivityUnitTestCase}.</p> 59 60<p>The {@link android.app.Activity} in an {@link android.test.ActivityUnitTestCase} 61is not automatically started by Android Instrumentation. To start the 62{@link android.app.Activity} in isolation, you need to explicitly call the 63{@link android.test.ActivityUnitTestCase#startActivity(android.content.Intent, android.os.Bundle, java.lang.Object) startActivity()} 64method, and pass in the {@link android.content.Intent} to 65launch your target {@link android.app.Activity}.</p> 66 67<p>For example:</p> 68<pre> 69public class LaunchActivityTest 70 extends ActivityUnitTestCase<LaunchActivity> { 71 ... 72 73 @Override 74 protected void setUp() throws Exception { 75 super.setUp(); 76 mLaunchIntent = new Intent(getInstrumentation() 77 .getTargetContext(), LaunchActivity.class); 78 startActivity(mLaunchIntent, null, null); 79 final Button launchNextButton = 80 (Button) getActivity() 81 .findViewById(R.id.launch_next_activity_button); 82 } 83} 84</pre> 85 86<h2 id="test_method">Validate Launch of Another Activity</h2> 87<p id="test_goals">Your unit testing goals might include:</p> 88<ul> 89<li>Verifying that {@code LaunchActivity} fires an 90{@link android.content.Intent} when a button is pushed clicked.</li> 91<li>Verifying that the launched {@link android.content.Intent} contains the 92correct payload data.</li> 93</ul> 94 95<p>To verify if an {@link android.content.Intent} was triggered 96following the {@link android.widget.Button} click, you can use the 97{@link android.test.ActivityUnitTestCase#getStartedActivityIntent()} method. 98By using assertion methods, you can verify that the returned 99{@link android.content.Intent} is not null, and that it contains the expected 100string value to launch the next {@link android.app.Activity}. If both assertions 101evaluate to {@code true}, you've successfully verified that the 102{@link android.content.Intent} was correctly sent by your 103{@link android.app.Activity}.</p> 104 105<p>You might implement your test method like this:</p> 106<pre> 107@MediumTest 108public void testNextActivityWasLaunchedWithIntent() { 109 startActivity(mLaunchIntent, null, null); 110 final Button launchNextButton = 111 (Button) getActivity() 112 .findViewById(R.id.launch_next_activity_button); 113 launchNextButton.performClick(); 114 115 final Intent launchIntent = getStartedActivityIntent(); 116 assertNotNull("Intent was null", launchIntent); 117 assertTrue(isFinishCalled()); 118 119 final String payload = 120 launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY); 121 assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD, payload); 122} 123</pre> 124<p>Because {@code LaunchActivity} runs in isolation, you cannot use the 125{@link android.test.TouchUtils} library to manipulate UI controls. To directly 126click a {@link android.widget.Button}, you can call the 127{@link android.view.View#performClick()} method instead.</p> 128 129 130 131 132 133 134 135