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