1 00:00:00,380 --> 00:00:02,100 It's time for testing. 2 00:00:02,100 --> 00:00:03,609 Over in the project pane, 3 00:00:03,609 --> 00:00:07,668 let's create a new test class by right-clicking on the test folder. 4 00:00:10,968 --> 00:00:16,400 New > Java Class, and let's name it MainActivityTest. 5 00:00:20,390 --> 00:00:23,740 And let's choose to add this and all other new files to gif. 6 00:00:26,910 --> 00:00:32,930 Now let's create our first Android test to test the functionality of our edit text. 7 00:00:32,930 --> 00:00:39,656 Let's create a new test method @Test public void and 8 00:00:39,656 --> 00:00:45,337 let's name it editTextUpdatesTextView and 9 00:00:45,337 --> 00:00:49,678 make sure it throws an exception. 10 00:00:53,378 --> 00:00:57,098 Let's start this method by commenting and the three As of testing. 11 00:00:57,098 --> 00:01:04,217 First, we arrange the objects under test, then we act on those objects and 12 00:01:04,217 --> 00:01:09,090 finally we assert that we get the expected result. 13 00:01:11,460 --> 00:01:13,330 Starting with the Arrange section, 14 00:01:13,330 --> 00:01:17,120 let's first create a new string variable named given string. 15 00:01:19,860 --> 00:01:22,880 And set it equal to test123. 16 00:01:25,430 --> 00:01:30,430 Then we need to call set text on our edit text and pass in our new string, 17 00:01:31,840 --> 00:01:36,850 but before we can do that, our edit text needs to exist and before our 18 00:01:36,850 --> 00:01:41,170 edit text can exist, main activity needs to have already been created. 19 00:01:42,300 --> 00:01:44,340 Sounds like we need an instance of our activity. 20 00:01:45,450 --> 00:01:48,660 Let's create a new main activity field at the top of the class. 21 00:01:50,810 --> 00:01:54,240 Main activity and we'll call it activity. 22 00:01:55,770 --> 00:01:59,848 Then let's create a new @Before method to set up our main activity. 23 00:02:02,379 --> 00:02:10,495 Let's name it setUp public void setUp and brackets. 24 00:02:10,495 --> 00:02:14,250 Next, inside our setUp method let's initialize our field 25 00:02:14,250 --> 00:02:15,836 to a new main activity object. 26 00:02:15,836 --> 00:02:20,540 Activity = new MainActivity. 27 00:02:20,540 --> 00:02:27,400 Then on the next line, let's call activity.onCreate and 28 00:02:27,400 --> 00:02:29,890 pass a null for the saved instance state. 29 00:02:30,910 --> 00:02:34,880 Saved instance state is always null the first time you start an app, so 30 00:02:34,880 --> 00:02:36,840 passing a null is no problem. 31 00:02:38,510 --> 00:02:41,530 Now that we've initialized our MainActivity object and 32 00:02:41,530 --> 00:02:45,190 called onCreate, our edit text should exist and 33 00:02:45,190 --> 00:02:49,200 we should be able to access it by using activity.edittext. 34 00:02:49,200 --> 00:02:55,850 Getting back to the Arrange section of our test, let's update our edit text 35 00:02:55,850 --> 00:03:04,400 by calling activity.editText.setText and 36 00:03:07,650 --> 00:03:09,100 passing in givenString. 37 00:03:10,170 --> 00:03:11,570 Nice. 38 00:03:11,570 --> 00:03:16,860 Moving on to the act section, we need a way to trigger our editText. 39 00:03:16,860 --> 00:03:21,810 Let's see how it's done in the activity, but instead of navigating to the activity, 40 00:03:21,810 --> 00:03:27,000 let's right click on main activity test up here and select split vertically. 41 00:03:28,330 --> 00:03:32,386 Then let's close the copy on the left side and 42 00:03:32,386 --> 00:03:39,129 now we can easily see main activity and main activity test at the same time. 43 00:03:39,129 --> 00:03:45,354 Looking over at main activity, if we want our text view to update, 44 00:03:45,354 --> 00:03:49,800 it looks like we need to call on editor action and 45 00:03:49,800 --> 00:03:55,263 pass in an action ID of editorInfo.IME_Action_Done. 46 00:03:55,263 --> 00:03:59,048 And luckily, we can do that pretty easily. 47 00:03:59,048 --> 00:04:03,916 Back in the act section of our test, 48 00:04:03,916 --> 00:04:11,225 just type activity.editText.onEditorAction and 49 00:04:11,225 --> 00:04:17,270 pass in editorInfo.IME_Action_Done. 50 00:04:17,270 --> 00:04:19,640 And that takes care of the act section. 51 00:04:19,640 --> 00:04:23,060 Now let's close MainActivity to give the whole screen back to our test. 52 00:04:25,560 --> 00:04:28,590 Finally, we just need to assert that our text view was 53 00:04:28,590 --> 00:04:30,870 updated with the correct text. 54 00:04:30,870 --> 00:04:38,158 Let's start by creating a new string variable named actualString. 55 00:04:38,158 --> 00:04:43,268 To store what's actually in our text view, and 56 00:04:43,268 --> 00:04:51,845 let's set it equal to activity.textView.getText().toString();. 57 00:04:51,845 --> 00:04:56,900 Then we just need to assert that our given string is equal to our actual string. 58 00:04:57,930 --> 00:05:04,040 On the next line let's type assertEquals and the parentheses 59 00:05:05,920 --> 00:05:10,970 and then use Alt+Enter to add the static import, and 60 00:05:10,970 --> 00:05:14,680 let's scroll a bit to choose one of these from the org.Junit package 61 00:05:17,480 --> 00:05:19,380 to stay consistent with our other imports. 62 00:05:20,890 --> 00:05:25,540 Then we just need to include our expected value, given string 63 00:05:27,150 --> 00:05:34,200 and then our actual value, actual string, add a semi colon and there we go. 64 00:05:34,200 --> 00:05:37,450 In the next video, we'll run our test and see what happens.