Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll write our very first Android unit test!
Related Links
-
0:00
It's time for testing.
-
0:02
Over in the project pane,
-
0:03
let's create a new test class by right-clicking on the test folder.
-
0:10
New > Java Class, and let's name it MainActivityTest.
-
0:20
And let's choose to add this and all other new files to gif.
-
0:26
Now let's create our first Android test to test the functionality of our edit text.
-
0:32
Let's create a new test method @Test public void and
-
0:39
let's name it editTextUpdatesTextView and
-
0:45
make sure it throws an exception.
-
0:53
Let's start this method by commenting and the three As of testing.
-
0:57
First, we arrange the objects under test, then we act on those objects and
-
1:04
finally we assert that we get the expected result.
-
1:11
Starting with the Arrange section,
-
1:13
let's first create a new string variable named given string.
-
1:19
And set it equal to test123.
-
1:25
Then we need to call set text on our edit text and pass in our new string,
-
1:31
but before we can do that, our edit text needs to exist and before our
-
1:36
edit text can exist, main activity needs to have already been created.
-
1:42
Sounds like we need an instance of our activity.
-
1:45
Let's create a new main activity field at the top of the class.
-
1:50
Main activity and we'll call it activity.
-
1:55
Then let's create a new @Before method to set up our main activity.
-
2:02
Let's name it setUp public void setUp and brackets.
-
2:10
Next, inside our setUp method let's initialize our field
-
2:14
to a new main activity object.
-
2:15
Activity = new MainActivity.
-
2:20
Then on the next line, let's call activity.onCreate and
-
2:27
pass a null for the saved instance state.
-
2:30
Saved instance state is always null the first time you start an app, so
-
2:34
passing a null is no problem.
-
2:38
Now that we've initialized our MainActivity object and
-
2:41
called onCreate, our edit text should exist and
-
2:45
we should be able to access it by using activity.edittext.
-
2:49
Getting back to the Arrange section of our test, let's update our edit text
-
2:55
by calling activity.editText.setText and
-
3:07
passing in givenString.
-
3:10
Nice.
-
3:11
Moving on to the act section, we need a way to trigger our editText.
-
3:16
Let's see how it's done in the activity, but instead of navigating to the activity,
-
3:21
let's right click on main activity test up here and select split vertically.
-
3:28
Then let's close the copy on the left side and
-
3:32
now we can easily see main activity and main activity test at the same time.
-
3:39
Looking over at main activity, if we want our text view to update,
-
3:45
it looks like we need to call on editor action and
-
3:49
pass in an action ID of editorInfo.IME_Action_Done.
-
3:55
And luckily, we can do that pretty easily.
-
3:59
Back in the act section of our test,
-
4:03
just type activity.editText.onEditorAction and
-
4:11
pass in editorInfo.IME_Action_Done.
-
4:17
And that takes care of the act section.
-
4:19
Now let's close MainActivity to give the whole screen back to our test.
-
4:25
Finally, we just need to assert that our text view was
-
4:28
updated with the correct text.
-
4:30
Let's start by creating a new string variable named actualString.
-
4:38
To store what's actually in our text view, and
-
4:43
let's set it equal to activity.textView.getText().toString();.
-
4:51
Then we just need to assert that our given string is equal to our actual string.
-
4:57
On the next line let's type assertEquals and the parentheses
-
5:05
and then use Alt+Enter to add the static import, and
-
5:10
let's scroll a bit to choose one of these from the org.Junit package
-
5:17
to stay consistent with our other imports.
-
5:20
Then we just need to include our expected value, given string
-
5:27
and then our actual value, actual string, add a semi colon and there we go.
-
5:34
In the next video, we'll run our test and see what happens.
You need to sign up for Treehouse in order to download course files.
Sign up