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 introduce Shadow Classes, and finish up our testing with Robolectric!
Arrange - Act - Assert Comments
// Arrange
// Act
// Assert
Related Links
-
0:00
It's time to finish up our Robolectric testing
-
0:02
by making sure that our launch activity button is working correctly.
-
0:06
Let's create a new test method at the bottom of the class and name it
-
0:12
buttonLaunchesOtherActivity.
-
0:19
And let's make sure it can throw an exception.
-
0:27
Then let's copy and paste in the arrange/act/assert comments and
-
0:33
get started arranging our test.
-
0:37
First, just like in main activity presenter test,
-
0:40
we'll need a class variable.
-
0:42
Class clazz = OtherActivity.class.
-
0:49
Next, in the act section, we need to click the button.
-
0:52
Let's type activity.launchActivityButton.callOnClick.
-
1:02
And finally for
-
1:03
the assert section, we just need to verify that we launched our other activity.
-
1:09
This is where things start to get a little bit tricky.
-
1:12
Remember, Robolectric isn't actually running our app.
-
1:16
So when we call the on click method of our launch activity button
-
1:20
It's not actually going to launch the other activity.
-
1:24
So instead of checking that we launched our other activity.
-
1:28
We'll just check that main activity calls start activity with the correct intent.
-
1:34
An easy way for us to do this with Robolectric is to use a Shadow Class.
-
1:40
A Shadow Class functions similarly to a mocked class,
-
1:43
like our mocked view we created earlier.
-
1:46
But behind the scenes, it's a little different.
-
1:49
When an Android framework class is first created, Robolectric looks for
-
1:53
a corresponding Shadow Class.
-
1:56
And if it finds one, it creates the corresponding shadow object.
-
2:01
Then, whenever a method is called on that framework class,
-
2:05
Robolectric first calls that method on the Shadow Class.
-
2:09
This way, the developers of Robolectric are able to capture
-
2:12
whatever is being called on the framework.
-
2:15
But, more importantly for us, it lets them add helpful methods to the shadow classes
-
2:20
to make testing less of a hassle.
-
2:22
One of these helpful methods from the Activity Shadow Class is called
-
2:26
getNextStartedActivity, which gives us null if there hasn't been a next activity,
-
2:33
or if there has it returns the intent of the next started activity.
-
2:38
Just what we're looking for.
-
2:40
So right below our assert comment, let's create a new ShadowActivity,
-
2:47
named shadowActivity.
-
2:51
And let's set it equal to Shadows.shadowOf and pass in our activity.
-
3:02
Alt+Enter to import shadows.
-
3:05
Then let's create a new Intent variable called actualIntent
-
3:13
and set it equal to, after importing the Intent class,
-
3:18
shadowActivity.getNextStartedActivity.
-
3:24
Now, we just need to create the expected intent,
-
3:26
which will do in the arrange section.
-
3:28
Intent, expected
-
3:35
intent and on second glance it looks like
-
3:39
this variable up here should probably be called actual color.
-
3:43
So let's change that real quick and
-
3:51
then back to our intent which equals a new intent and
-
3:57
we can pass in our activity for the context.
-
4:02
And then our clazz variable or the class.
-
4:06
And last but
-
4:07
not least, let's assert that the actual intent matches the expected intent.
-
4:11
assertEquals(expectedIntent, actualIntent),
-
4:21
nice.
-
4:22
Now let's comment out the act section so we can see the test fail.
-
4:29
Then let's right-click and run our test.
-
4:37
And just like we expected, it failed.
-
4:40
There was no next started activity, and we ended up with a null intent.
-
4:45
Now let's uncomment out the act section and run our test again.
-
5:00
And what?
-
5:02
It failed again?
-
5:03
If we look at the differences between these two Intents
-
5:09
they look basically the same, but
-
5:13
it does look like there might be an extra space on the left side here.
-
5:19
Either way here's what we need to know.
-
5:21
Intents can have small differences which can make two
-
5:25
seemingly identical Intents not actually equal to one another.
-
5:30
But using the filter equals method of the Intent class,
-
5:34
lets us ignore the small differences and instead
-
5:37
just tells us if the two Intents are equivalent from a functional standpoint.
-
5:42
Let's delete the assert statement and
-
5:44
rewrite it to instead make use of this filter equals method.
-
5:52
AssertTrue(expectedIntent.filterEquals(ac- tualIntent)).
-
6:02
Then let's run the test again.
-
6:09
And [LAUGH] there we go!
-
6:10
A passing test.
-
6:12
Finally let's right click somewhere at the top of the class and choose
-
6:20
run main activity test to run all 3 tests at once and
-
6:25
what do we get 3 passing test
-
6:30
Robolectric is a supremely cool way to test your Android apps.
-
6:35
It lets us write tests as if there's an actual device there
-
6:39
even though there's not but that's not always the best solution.
-
6:43
Sometimes you just can't get around using an actual device and the next stage.
-
6:48
We'll see how we can test the UI directly using espresso.
You need to sign up for Treehouse in order to download course files.
Sign up