Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll take our first look at the Robolectric Android unit testing framework!
Gradle Pt. 1
testCompile 'org.robolectric:robolectric:3.0'
Github Issue
Gradle Pt. 2
UPDATE: Robolectric 3.1 has now been officially released. Instead of using a snapshot version of Robolectric we should now use version 3.1
testCompile 'org.robolectric:robolectric:3.1'
Related Links
[MUSIC]
0:00
We've just finished using
the Kito to unit test our app.
0:04
But now it's time to see what's so
special about Robolectric.
0:08
To quote their website, Robolectric it
is a unit test framework that defangs
0:11
the Android S.T.K. jar so you can test
drive the development of your Android app.
0:17
Remember earlier when we were trying to
start our activity and it didn't work.
0:22
Roboelectric aims to fix that
by rewriting the Android classes
0:27
as they're being loaded
making it possible for
0:31
them to run on your computer just
like any other Java application.
0:34
First things first we need to add
Roboelectric as a dependency.
0:39
Let's head over to our
build.gradle file and add it.
0:43
It's in the teacher's notes below.
0:47
Then hit Sync Now and we're good to go.
0:52
Now let's get started with Robolectric
by switching back to our main activity
1:00
test class.
1:04
Then let's comment out the lines
in both the set up and
1:07
editTextUpdatesTextView methods.
1:10
Just like when we use my Kito we need
to be sure that we're using the correct
1:19
test runner.
1:24
Let's add a run with annotation
right above our class declaration
1:25
and then pass an Alt+enter to import it,
and
1:33
we're passing in
RobolectricGradleTestRunner.class.
1:37
At this point if we right click and
1:41
try to run main activity test,
we'll get an error.
1:47
Field constants not specified
in @Config annotation.
1:53
This is required when using
RobolectricGradleTestRunner!
1:59
So right below our RunWith annotation,
let's add the Config annotation.
2:03
And inside the parentheses,
2:13
let's specify the Constants field and
2:17
set it equal to BuildConfig.class.
2:23
Roboelectric use the build config class
to figure out how to access our manifest,
2:29
resources and assets.
2:34
So it's important we add that.
2:36
Roboelectric would have a tough time being
useful without knowing where anything is.
2:39
Now that we've specified the constants
field, let's try running this again.
2:44
Darn another error.
2:51
Turns out Roboelectric doesn't yet
support A.P.I. level twenty three.
2:53
Luckily we can fix this pretty easily
2:58
by adding another field
to our config annotation.
3:01
After Bill.class let's add
3:05
comma Sdk equals Build.VERSION_CODES and
3:10
Alt+Enter to import build .LOLLIPOP.
3:19
Then let's try running it again.
3:26
And after it finishes downloading
some things, success!
3:36
But why do we need to use Lollipop?
3:43
Why can't we just use Marshmellow?
3:46
Well after a new version of Android
is released it just takes a while for
3:49
Roboelectric to get updated.
3:53
If you're curious about
why the process takes so
3:56
long, check out the GitHub issue
linked in the teacher's notes below.
3:58
Also the newest version of Roboelectric
might just work with Marshmellow.
4:02
It just isn't officially released yet
but that doesn't mean we can't use it.
4:07
Over on our build.gradle file,
4:13
let's change 3.0 to 3.1-SNAPSHOT.
4:18
A snapshot build represents
the most recent changes and
4:27
isn't considered a stable release.
4:31
To use this snapshot build,
4:34
we'll also need to add a repository
section above the dependency section.
4:35
Let's copy it in from
the teacher's notes below and
4:41
then paste it right above
the word dependencies.
4:45
Then let's sync the project and
now thanks to our new snapshot
4:49
build we should be able to safely
remove the SDK config parameter.
4:55
And it works.
5:12
Nice.
We're all set up to start exploring unit
5:13
testing with Roboelectric.
5:17
In the next video, will see how to set
up an activity for unit testing and
5:19
write our first Robolectric unit test.
5:23
You need to sign up for Treehouse in order to download course files.
Sign up