1 00:00:00,330 --> 00:00:04,531 We just changed our app to include a new interface and a new class. 2 00:00:04,531 --> 00:00:07,880 And we're almost ready to take another shot at testing. 3 00:00:07,880 --> 00:00:11,590 But first let's take a look back at what we accomplished in the last video. 4 00:00:13,270 --> 00:00:17,910 We started by creating the MainActivityView interface, which defines 5 00:00:17,910 --> 00:00:22,686 all the actions we can take that affect the UI, changing the TextViewText, 6 00:00:22,686 --> 00:00:27,490 changing the BackgroundColor, and launching the OtherActivity. 7 00:00:27,490 --> 00:00:31,060 We also created the MainActivityPresenter class. 8 00:00:31,060 --> 00:00:34,550 This class is responsible for presenting the view and 9 00:00:34,550 --> 00:00:39,060 is the only class allowed to call methods defined in our MainActivityView interface. 10 00:00:40,360 --> 00:00:42,420 To make this work with our activity, 11 00:00:42,420 --> 00:00:47,670 we start by making our activity implement the MainActivityView interface. 12 00:00:47,670 --> 00:00:51,150 Then we initialize our MainActivityPresenter, and 13 00:00:51,150 --> 00:00:53,100 whenever an action occurs, 14 00:00:53,100 --> 00:00:57,230 like a user clicking the launch activity button, we let the presenter handle it. 15 00:00:58,460 --> 00:01:02,740 This way of organizing the app makes it a lot easier to unit test. 16 00:01:02,740 --> 00:01:07,880 Now instead of checking the text of the text view directly, we can just check and 17 00:01:07,880 --> 00:01:11,300 make sure that our presenter is calling changeTextViewText 18 00:01:11,300 --> 00:01:13,180 with the right argument. 19 00:01:13,180 --> 00:01:15,980 We don't need to involve the activity at all. 20 00:01:15,980 --> 00:01:19,590 We just need to test that when we call a method on the presenter, 21 00:01:19,590 --> 00:01:21,420 it calls the right method on the view. 22 00:01:23,000 --> 00:01:30,036 So instead of testing MainActivity, let's instead test MainActivityPresenter. 23 00:01:30,036 --> 00:01:34,860 Right-click, Go To > Test. 24 00:01:34,860 --> 00:01:35,780 Create New Test. 25 00:01:37,600 --> 00:01:40,354 Then let's check the box for a setUp method and 26 00:01:40,354 --> 00:01:44,031 check these boxes to make it generate all three test methods. 27 00:01:47,197 --> 00:01:50,764 Then hit OK and make sure to pick the test directory and 28 00:01:50,764 --> 00:01:53,002 not the androidTest directory. 29 00:01:53,002 --> 00:01:56,962 The androidTest directory is for tests that run on an actual device. 30 00:01:59,496 --> 00:02:01,117 Once our tests are generated, 31 00:02:01,117 --> 00:02:04,868 let's quickly change the test methods to not start with the word test. 32 00:02:12,996 --> 00:02:16,016 Then, since we'll be using our presenter, 33 00:02:16,016 --> 00:02:20,636 let's create a new MainActivityPresenter field named presenter. 34 00:02:25,356 --> 00:02:28,214 But before we can initialize this presenter, 35 00:02:28,214 --> 00:02:30,370 we will need a MainActivityView. 36 00:02:30,370 --> 00:02:36,320 Let's create a new field for a MainActivityView and name it view. 37 00:02:37,690 --> 00:02:42,490 Right now the only main activity view we have is main activity, 38 00:02:42,490 --> 00:02:44,180 which we're trying to avoid using. 39 00:02:45,540 --> 00:02:50,691 So let's instead create a new inner class named MockedView and 40 00:02:50,691 --> 00:02:55,077 make it implement our MainActivityView interface. 41 00:02:57,726 --> 00:02:59,740 And Alt+Enter to add the methods. 42 00:03:02,160 --> 00:03:04,984 Now that we've got MockedView as an inner class, 43 00:03:04,984 --> 00:03:08,090 in the setPp method let's initialize our view field. 44 00:03:10,140 --> 00:03:13,295 view = new MockedView, and 45 00:03:13,295 --> 00:03:18,821 then use this field to initialize our presenter. 46 00:03:18,821 --> 00:03:25,114 presenter = new MainActivityPresenter and passing in our view. 47 00:03:26,976 --> 00:03:29,610 All right, we're all set up. 48 00:03:29,610 --> 00:03:31,810 Let's start with the editTextUpdated test. 49 00:03:33,000 --> 00:03:35,265 First we need to arrange the test. 50 00:03:40,375 --> 00:03:43,856 Let's create a new string variable named givenString and 51 00:03:43,856 --> 00:03:46,210 set it equal to "test123" again. 52 00:03:48,810 --> 00:03:52,093 Then we need to act. 53 00:03:52,093 --> 00:03:55,632 Let's type presenter.editTextUpdated and 54 00:03:55,632 --> 00:04:00,628 pass in our string to simulate the user submitting the edit text. 55 00:04:04,986 --> 00:04:10,060 Lastly we need to assert that we got the right outcome, meaning we need 56 00:04:10,060 --> 00:04:15,134 to check that the value we passed into our presenter is the same value we 57 00:04:15,134 --> 00:04:20,080 got passed into the changeTextViewText method of our MockedView. 58 00:04:20,080 --> 00:04:24,225 To do this let's create a new string field in our MockedView class, 59 00:04:27,816 --> 00:04:30,002 And name it textViewText. 60 00:04:33,521 --> 00:04:37,046 Then, in the changeTextViewText method, 61 00:04:37,046 --> 00:04:42,770 let's update our new TextViewText field to equal the text parameter. 62 00:04:44,700 --> 00:04:48,158 Now back in the editTextUpdated method, 63 00:04:48,158 --> 00:04:52,716 let's add a line at the bottom and type assertEquals. 64 00:04:56,063 --> 00:05:00,607 And then for the expected result, let's pass in givenString. 65 00:05:00,607 --> 00:05:04,809 And for the actual result, let's pass in the TextViewText field from our 66 00:05:04,809 --> 00:05:08,085 MockedView, which we'll need to cast to a MockedView. 67 00:05:09,820 --> 00:05:15,757 ((MockedView), casting our view to a MockedView, 68 00:05:15,757 --> 00:05:18,670 and then .textViewText. 69 00:05:21,411 --> 00:05:23,340 Great work! 70 00:05:23,340 --> 00:05:26,612 Now let's right-click inside this method and 71 00:05:26,612 --> 00:05:30,627 pick Run 'editTextUpdated()' and see what happens. 72 00:05:30,627 --> 00:05:31,427 Woohoo! 73 00:05:31,427 --> 00:05:33,490 We passed! 74 00:05:33,490 --> 00:05:38,120 Now I know some of you are probably thinking that this doesn't really count. 75 00:05:38,120 --> 00:05:41,690 And I'll admit that it does look a bit trivial. 76 00:05:41,690 --> 00:05:45,060 But if you think about it, we've tested everything that we need to test for 77 00:05:45,060 --> 00:05:46,590 our edit text. 78 00:05:46,590 --> 00:05:53,410 On the front-end, we don't need to test that the OnEditorAction method works. 79 00:05:53,410 --> 00:05:57,550 We just need to drop in a call to presenter.editTextUpdated and 80 00:05:57,550 --> 00:06:00,190 trust that the Android system works as advertised. 81 00:06:01,260 --> 00:06:04,512 Similarly, after introducing the view and presenter, 82 00:06:04,512 --> 00:06:07,840 we don't actually need to check the text in our text view. 83 00:06:10,076 --> 00:06:13,519 We just need to check that changeTextViewText was called with 84 00:06:13,519 --> 00:06:15,490 the right value. 85 00:06:15,490 --> 00:06:19,750 We can trust that this textView.setText line works as expected. 86 00:06:21,090 --> 00:06:22,040 We did it. 87 00:06:22,040 --> 00:06:25,015 We restructured our project to be more testable and 88 00:06:25,015 --> 00:06:27,239 implemented a test using only JUnit. 89 00:06:27,239 --> 00:06:31,057 In the next video we'll see how we can start to simplify things by making use of 90 00:06:31,057 --> 00:06:33,481 a very popular mocking framework called Mockito