Push the Button3:50 with Ben Deitch
In this video we finish up by UI testing our Button!
We've just finished testing our edit text and our spinner using Espresso. 0:00 All that remains is testing that our button launches our other activity, but 0:04 I'm pretty sure that won't be left remaining for 0:08 long this time instead of checking something about the intent. 0:11 Let's check something specific about our other activity. 0:15 If we open up activity_other.xml. 0:20 It looks like we should be able to match on this other activity text. 0:27 And if we do find a match with this text, 0:33 it means that this TextView is on the screen. 0:36 Which means our Other Activity has successfully launched. 0:39 So for this test, all we're going to do is click on the button and 0:43 then check that there exists a view with text equal to Other Activity. 0:47 Starting with the Arrange section, 0:56 let's create a string variable to store the text, we'd like to find. 0:58 String otherActivityString = "Other Activity". 1:04 Next in the Act section, 1:12 let's click our button, 1:16 onView(withID(R.id.launchActivityButton)) then 1:20 .perform and click. 1:29 Finally in the Assert section, 1:33 let's get our view with text equal to otherActivityString. 1:35 So onView(withText(otherActivityString)), 1:40 then let's check that this matches a not null value. 1:48 And that's it. 2:02 Let's run our test and see if it works. 2:03 Nice. 2:16 Now let's comment out the Act section and make sure it fails. 2:17 And we get a NoMatchingViewException. 2:36 Perfect, no views in hierarchy found matching: with text: is "Other Activity". 2:39 Now let's bring back the Act section and take a couple of victory laps. 2:47 Nice. 3:00 Well you go. 3:01 One more, one more. 3:04 Six seconds later and I've already watched my app get automatically tested twice. 3:10 Using tests and following test driven design isn't always easy, 3:16 especially in the world of Android. 3:20 But now that you're familiar with Mocito, Roboletric, and 3:23 Espresso, you've got the power to test anything your heart desires. 3:26 Unit testing with no Android framework? 3:31 Try Mocito. 3:33 If you need the framework give Robolectric a shot, 3:34 and if you want to run a test on an actual device, well, Espresso's got you covered. 3:37 Maybe now would be a good time to look back at some of your earlier projects. 3:43 I'm sure they could use some testing. 3:47 Until next time. 3:48
You need to sign up for Treehouse in order to download course files.Sign up