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 start testing our project with Robolectric!
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
We've added a dependency, changed our test
runner, and set up the configuration.
0:00
So without further ado,
let's get back to testing.
0:05
Unlike Mockito,
Robolectric doesn't require us to
0:09
use the view presenter model
that we've been using.
0:12
With Robolectric,
0:15
we can just test the activity directly,
hence, MainActivityTest.
0:16
Let's start by uncommenting
the setUp method and
0:22
deleting everything except activity =.
0:28
Then let's set it equal to
Robolectric.setupActivity and
0:33
pass in MainActivity.class for
0:41
the parameter, and that's it.
0:44
Thanks to Robolectric,
our activity now exists and
0:49
has gone through the onCreate,
onStart and onResume life cycle methods.
0:52
Moving on to the editTextUpdatesTextView
test, let's start and
0:59
finish by uncommenting it.
1:03
That button works better for that.
1:11
Then let's run our tests again and,
1:13
yikes, what's this all about?
1:17
Well, it turns out that currently
the snapshot build isn't quite ready for
1:27
Marshmallow.
1:32
So let's add back in the Config SDK
1:34
parameter to run on Lollipop instead,
1:38
comma sdk = Build.VERSION_CODES.LOLLIPOP.
1:43
We'll keep using this snapshot build,
though.
1:49
It fixes a couple of bugs we would
otherwise run into down the road.
1:52
Then let's try running
our tests one more time.
1:57
And success!
2:05
But wait,
how do we know this is really working?
2:07
Let's comment out the Act line and
run it again.
2:10
Just like we would expect, the test now
fails because we didn't change the text.
2:22
It still has the default value
of textView set in the layout.
2:28
All right, let's uncomment out
the Act line and run it again.
2:32
This is what makes Robolectric so
powerful.
2:40
We can write our tests as if our app is
actually running, even though it's not.
2:43
It just seems that way,
thanks to Robolectric.
2:48
Now that we've got our editText tested,
let's move on to our spinner.
2:52
Let's create a new @Test method.
2:58
Public void, and let's name it
spinnerUpdatesBackgroundColor.
3:07
Which throws an exception.
3:17
Then let's copy and
paste in the Arrange, Act,
3:22
Assert comments from
the teacher's notes below.
3:24
Cool, starting with the Arrange section,
3:34
it will be exactly the same as it
is in MainActivity presenter test.
3:36
So let's just copy and paste it in.
3:41
Moving on to the Act section, we need to
select the right item from our spinner.
3:56
Let's type activity.colorSpinner
to get our spinner object,
4:01
then let's call the setSelection
method and pass in our index.
4:07
Now we just need to assert
that the background color is
4:17
equal to our given color.
4:20
But before we get to
the Assert statement itself,
4:22
let's create a variable to
hold the background color,
4:27
int expectedColor and
let's set it equal to
4:32
activity.LinearLayout.getBackground to
get the background.
4:36
Then if we open up the quick
documentation with Ctrl + J on Mac or
4:43
Ctrl + Q on Windows,
4:48
we can see that the getBackground
method will return a Drawable.
4:50
But in our case,
we can be a little more specific.
4:57
Since our background is
just one solid color,
5:00
instead of Android using
the regular Drawable class,
5:04
it uses a specialized version of
the Drawable class named ColorDrawable.
5:07
Let's cast this object to
a ColorDrawable object, ColorDrawable,
5:14
and then add parenthesis around
the whole right side of our statement.
5:19
Then let's call getColor on our
newly cast ColorDrawable object.
5:29
And there we go.
5:35
We retrieved the background
color from our linear layout and
5:37
stored it in this expectedColor variable.
5:40
To finish up,
5:43
we just need to assert that the given
color matches the expected color.
5:44
AssertEquals givenColor, expectedColor.
5:54
All right,
now let's comment out the Act section
6:00
And then right-click inside our test
method and select Run to run the test.
6:07
And now that we've seen the test fail,
let's bring back the Act section and,
6:16
hopefully, we'll see the test pass.
6:20
Nice, with our spinner test out of the
way, all that's left is testing that our
6:30
Launch Activity button successfully
launches our other activity,
6:34
which we'll get to in the next video.
6:38
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