Testing the Presenter6:34 with Ben Deitch
In this video we'll start adding tests to our Presenter!
We just changed our app to include a new interface and a new class. 0:00 And we're almost ready to take another shot at testing. 0:04 But first let's take a look back at what we accomplished in the last video. 0:07 We started by creating the MainActivityView interface, which defines 0:13 all the actions we can take that affect the UI, changing the TextViewText, 0:17 changing the BackgroundColor, and launching the OtherActivity. 0:22 We also created the MainActivityPresenter class. 0:27 This class is responsible for presenting the view and 0:31 is the only class allowed to call methods defined in our MainActivityView interface. 0:34 To make this work with our activity, 0:40 we start by making our activity implement the MainActivityView interface. 0:42 Then we initialize our MainActivityPresenter, and 0:47 whenever an action occurs, 0:51 like a user clicking the launch activity button, we let the presenter handle it. 0:53 This way of organizing the app makes it a lot easier to unit test. 0:58 Now instead of checking the text of the text view directly, we can just check and 1:02 make sure that our presenter is calling changeTextViewText 1:07 with the right argument. 1:11 We don't need to involve the activity at all. 1:13 We just need to test that when we call a method on the presenter, 1:15 it calls the right method on the view. 1:19 So instead of testing MainActivity, let's instead test MainActivityPresenter. 1:23 Right-click, Go To > Test. 1:30 Create New Test. 1:34 Then let's check the box for a setUp method and 1:37 check these boxes to make it generate all three test methods. 1:40 Then hit OK and make sure to pick the test directory and 1:47 not the androidTest directory. 1:50 The androidTest directory is for tests that run on an actual device. 1:53 Once our tests are generated, 1:59 let's quickly change the test methods to not start with the word test. 2:01 Then, since we'll be using our presenter, 2:12 let's create a new MainActivityPresenter field named presenter. 2:16 But before we can initialize this presenter, 2:25 we will need a MainActivityView. 2:28 Let's create a new field for a MainActivityView and name it view. 2:30 Right now the only main activity view we have is main activity, 2:37 which we're trying to avoid using. 2:42 So let's instead create a new inner class named MockedView and 2:45 make it implement our MainActivityView interface. 2:50 And Alt+Enter to add the methods. 2:57 Now that we've got MockedView as an inner class, 3:02 in the setPp method let's initialize our view field. 3:04 view = new MockedView, and 3:10 then use this field to initialize our presenter. 3:13 presenter = new MainActivityPresenter and passing in our view. 3:18 All right, we're all set up. 3:26 Let's start with the editTextUpdated test. 3:29 First we need to arrange the test. 3:33 Let's create a new string variable named givenString and 3:40 set it equal to "test123" again. 3:43 Then we need to act. 3:48 Let's type presenter.editTextUpdated and 3:52 pass in our string to simulate the user submitting the edit text. 3:55 Lastly we need to assert that we got the right outcome, meaning we need 4:04 to check that the value we passed into our presenter is the same value we 4:10 got passed into the changeTextViewText method of our MockedView. 4:15 To do this let's create a new string field in our MockedView class, 4:20 And name it textViewText. 4:27 Then, in the changeTextViewText method, 4:33 let's update our new TextViewText field to equal the text parameter. 4:37 Now back in the editTextUpdated method, 4:44 let's add a line at the bottom and type assertEquals. 4:48 And then for the expected result, let's pass in givenString. 4:56 And for the actual result, let's pass in the TextViewText field from our 5:00 MockedView, which we'll need to cast to a MockedView. 5:04 ((MockedView), casting our view to a MockedView, 5:09 and then .textViewText. 5:15 Great work! 5:21 Now let's right-click inside this method and 5:23 pick Run 'editTextUpdated()' and see what happens. 5:26 Woohoo! 5:30 We passed! 5:31 Now I know some of you are probably thinking that this doesn't really count. 5:33 And I'll admit that it does look a bit trivial. 5:38 But if you think about it, we've tested everything that we need to test for 5:41 our edit text. 5:45 On the front-end, we don't need to test that the OnEditorAction method works. 5:46 We just need to drop in a call to presenter.editTextUpdated and 5:53 trust that the Android system works as advertised. 5:57 Similarly, after introducing the view and presenter, 6:01 we don't actually need to check the text in our text view. 6:04 We just need to check that changeTextViewText was called with 6:10 the right value. 6:13 We can trust that this textView.setText line works as expected. 6:15 We did it. 6:21 We restructured our project to be more testable and 6:22 implemented a test using only JUnit. 6:25 In the next video we'll see how we can start to simplify things by making use of 6:27 a very popular mocking framework called Mockito 6:31
You need to sign up for Treehouse in order to download course files.Sign up