1 00:00:00,000 --> 00:00:04,618 [MUSIC] 2 00:00:04,618 --> 00:00:08,190 We've just finished using the Kito to unit test our app. 3 00:00:08,190 --> 00:00:11,990 But now it's time to see what's so special about Robolectric. 4 00:00:11,990 --> 00:00:17,140 To quote their website, Robolectric it is a unit test framework that defangs 5 00:00:17,140 --> 00:00:21,950 the Android S.T.K. jar so you can test drive the development of your Android app. 6 00:00:22,970 --> 00:00:27,975 Remember earlier when we were trying to start our activity and it didn't work. 7 00:00:27,975 --> 00:00:31,880 Roboelectric aims to fix that by rewriting the Android classes 8 00:00:31,880 --> 00:00:34,340 as they're being loaded making it possible for 9 00:00:34,340 --> 00:00:37,710 them to run on your computer just like any other Java application. 10 00:00:39,130 --> 00:00:43,250 First things first we need to add Roboelectric as a dependency. 11 00:00:43,250 --> 00:00:45,680 Let's head over to our build.gradle file and add it. 12 00:00:47,540 --> 00:00:48,618 It's in the teacher's notes below. 13 00:00:52,958 --> 00:00:59,760 Then hit Sync Now and we're good to go. 14 00:01:00,810 --> 00:01:04,510 Now let's get started with Robolectric by switching back to our main activity 15 00:01:04,510 --> 00:01:05,420 test class. 16 00:01:07,060 --> 00:01:10,165 Then let's comment out the lines in both the set up and 17 00:01:10,165 --> 00:01:12,383 editTextUpdatesTextView methods. 18 00:01:19,993 --> 00:01:24,085 Just like when we use my Kito we need to be sure that we're using the correct 19 00:01:24,085 --> 00:01:24,885 test runner. 20 00:01:25,915 --> 00:01:29,445 Let's add a run with annotation right above our class declaration 21 00:01:33,385 --> 00:01:37,540 and then pass an Alt+enter to import it, and 22 00:01:37,540 --> 00:01:41,980 we're passing in RobolectricGradleTestRunner.class. 23 00:01:41,980 --> 00:01:47,190 At this point if we right click and 24 00:01:47,190 --> 00:01:51,810 try to run main activity test, we'll get an error. 25 00:01:53,630 --> 00:01:57,980 Field constants not specified in @Config annotation. 26 00:01:59,060 --> 00:02:02,640 This is required when using RobolectricGradleTestRunner! 27 00:02:03,920 --> 00:02:09,468 So right below our RunWith annotation, let's add the Config annotation. 28 00:02:13,048 --> 00:02:17,501 And inside the parentheses, 29 00:02:17,501 --> 00:02:23,378 let's specify the Constants field and 30 00:02:23,378 --> 00:02:29,135 set it equal to BuildConfig.class. 31 00:02:29,135 --> 00:02:34,070 Roboelectric use the build config class to figure out how to access our manifest, 32 00:02:34,070 --> 00:02:36,810 resources and assets. 33 00:02:36,810 --> 00:02:38,030 So it's important we add that. 34 00:02:39,055 --> 00:02:43,160 Roboelectric would have a tough time being useful without knowing where anything is. 35 00:02:44,420 --> 00:02:47,808 Now that we've specified the constants field, let's try running this again. 36 00:02:51,128 --> 00:02:53,890 Darn another error. 37 00:02:53,890 --> 00:02:58,780 Turns out Roboelectric doesn't yet support A.P.I. level twenty three. 38 00:02:58,780 --> 00:03:01,498 Luckily we can fix this pretty easily 39 00:03:01,498 --> 00:03:05,274 by adding another field to our config annotation. 40 00:03:05,274 --> 00:03:10,808 After Bill.class let's add 41 00:03:10,808 --> 00:03:19,220 comma Sdk equals Build.VERSION_CODES and 42 00:03:19,220 --> 00:03:26,973 Alt+Enter to import build .LOLLIPOP. 43 00:03:26,973 --> 00:03:28,153 Then let's try running it again. 44 00:03:36,208 --> 00:03:43,960 And after it finishes downloading some things, success! 45 00:03:43,960 --> 00:03:46,830 But why do we need to use Lollipop? 46 00:03:46,830 --> 00:03:48,170 Why can't we just use Marshmellow? 47 00:03:49,260 --> 00:03:53,700 Well after a new version of Android is released it just takes a while for 48 00:03:53,700 --> 00:03:55,130 Roboelectric to get updated. 49 00:03:56,130 --> 00:03:58,110 If you're curious about why the process takes so 50 00:03:58,110 --> 00:04:01,370 long, check out the GitHub issue linked in the teacher's notes below. 51 00:04:02,440 --> 00:04:07,600 Also the newest version of Roboelectric might just work with Marshmellow. 52 00:04:07,600 --> 00:04:11,780 It just isn't officially released yet but that doesn't mean we can't use it. 53 00:04:13,180 --> 00:04:18,193 Over on our build.gradle file, 54 00:04:18,193 --> 00:04:25,000 let's change 3.0 to 3.1-SNAPSHOT. 55 00:04:27,840 --> 00:04:31,310 A snapshot build represents the most recent changes and 56 00:04:31,310 --> 00:04:33,010 isn't considered a stable release. 57 00:04:34,200 --> 00:04:35,545 To use this snapshot build, 58 00:04:35,545 --> 00:04:40,370 we'll also need to add a repository section above the dependency section. 59 00:04:41,460 --> 00:04:45,700 Let's copy it in from the teacher's notes below and 60 00:04:45,700 --> 00:04:48,220 then paste it right above the word dependencies. 61 00:04:49,850 --> 00:04:55,346 Then let's sync the project and now thanks to our new snapshot 62 00:04:55,346 --> 00:05:01,274 build we should be able to safely remove the SDK config parameter. 63 00:05:12,890 --> 00:05:13,950 And it works. 64 00:05:13,950 --> 00:05:17,232 Nice. We're all set up to start exploring unit 65 00:05:17,232 --> 00:05:19,640 testing with Roboelectric. 66 00:05:19,640 --> 00:05:23,280 In the next video, will see how to set up an activity for unit testing and 67 00:05:23,280 --> 00:05:25,420 write our first Robolectric unit test.