1 00:00:00,420 --> 00:00:04,199 We've just finished testing our edit text and our spinner using Espresso. 2 00:00:04,199 --> 00:00:08,768 All that remains is testing that our button launches our other activity, but 3 00:00:08,768 --> 00:00:11,720 I'm pretty sure that won't be left remaining for 4 00:00:11,720 --> 00:00:15,970 long this time instead of checking something about the intent. 5 00:00:15,970 --> 00:00:18,750 Let's check something specific about our other activity. 6 00:00:20,020 --> 00:00:22,599 If we open up activity_other.xml. 7 00:00:27,959 --> 00:00:32,470 It looks like we should be able to match on this other activity text. 8 00:00:33,670 --> 00:00:36,480 And if we do find a match with this text, 9 00:00:36,480 --> 00:00:39,620 it means that this TextView is on the screen. 10 00:00:39,620 --> 00:00:43,640 Which means our Other Activity has successfully launched. 11 00:00:43,640 --> 00:00:47,746 So for this test, all we're going to do is click on the button and 12 00:00:47,746 --> 00:00:52,250 then check that there exists a view with text equal to Other Activity. 13 00:00:56,030 --> 00:00:58,030 Starting with the Arrange section, 14 00:00:58,030 --> 00:01:01,910 let's create a string variable to store the text, we'd like to find. 15 00:01:04,030 --> 00:01:12,441 String otherActivityString = "Other Activity". 16 00:01:12,441 --> 00:01:16,278 Next in the Act section, 17 00:01:16,278 --> 00:01:20,299 let's click our button, 18 00:01:20,299 --> 00:01:29,624 onView(withID(R.id.launchActivityButton)) then 19 00:01:29,624 --> 00:01:33,110 .perform and click. 20 00:01:33,110 --> 00:01:35,240 Finally in the Assert section, 21 00:01:35,240 --> 00:01:38,865 let's get our view with text equal to otherActivityString. 22 00:01:40,770 --> 00:01:48,888 So onView(withText(otherActivityString)), 23 00:01:48,888 --> 00:01:57,010 then let's check that this matches a not null value. 24 00:02:02,490 --> 00:02:03,910 And that's it. 25 00:02:03,910 --> 00:02:05,559 Let's run our test and see if it works. 26 00:02:16,419 --> 00:02:17,670 Nice. 27 00:02:17,670 --> 00:02:20,200 Now let's comment out the Act section and make sure it fails. 28 00:02:36,700 --> 00:02:39,550 And we get a NoMatchingViewException. 29 00:02:39,550 --> 00:02:45,250 Perfect, no views in hierarchy found matching: with text: is "Other Activity". 30 00:02:47,010 --> 00:02:52,239 Now let's bring back the Act section and take a couple of victory laps. 31 00:03:00,239 --> 00:03:00,739 Nice. 32 00:03:01,890 --> 00:03:02,450 Well you go. 33 00:03:04,010 --> 00:03:04,619 One more, one more. 34 00:03:10,640 --> 00:03:16,800 Six seconds later and I've already watched my app get automatically tested twice. 35 00:03:16,800 --> 00:03:20,560 Using tests and following test driven design isn't always easy, 36 00:03:20,560 --> 00:03:23,310 especially in the world of Android. 37 00:03:23,310 --> 00:03:26,590 But now that you're familiar with Mocito, Roboletric, and 38 00:03:26,590 --> 00:03:31,120 Espresso, you've got the power to test anything your heart desires. 39 00:03:31,120 --> 00:03:33,210 Unit testing with no Android framework? 40 00:03:33,210 --> 00:03:34,610 Try Mocito. 41 00:03:34,610 --> 00:03:37,707 If you need the framework give Robolectric a shot, 42 00:03:37,707 --> 00:03:43,350 and if you want to run a test on an actual device, well, Espresso's got you covered. 43 00:03:43,350 --> 00:03:47,040 Maybe now would be a good time to look back at some of your earlier projects. 44 00:03:47,040 --> 00:03:48,930 I'm sure they could use some testing. 45 00:03:48,930 --> 00:03:49,730 Until next time.