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
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll see how we can use Mockito to simplify all of our mocking needs!
Gradle
- testCompile "org.mockito:mockito-core:1.+"
Arrange - Act - Assert Comments
// Arrange
// Act
// Assert
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
In the last video we finally finished
writing a unit test that didn't fail.
0:00
but to make that unit test work,
0:04
we had to create a mock view implementing
our main activity view interface.
0:06
Not that there's anything wrong with that.
0:10
But it is a lot of code just to
verify that we're calling a method
0:12
with some value.
0:16
Instead, let's see how we
can use the Mackito library
0:17
to condense this into something
a bit more manageable.
0:20
The first step in using the Mackito
library is to include it in our project.
0:25
Let's open up our build.gradle file and
0:29
add the pendency shown in
the teachers' notes below.
0:31
Then let's hit sync now and
we're good to go.
0:38
Back in the main activity
presenter test class.
0:46
Let's refactor this to
now use my Kito First
0:50
let's add a line above this class and type
0:53
@RunWith(Mockito)JUnitRunner.class.
0:57
Now, instead of running our tests
with the default JUnitRunner.
1:06
we'll be running them with the Runner
taking full advantage of what Mockito
1:10
has to offer.
1:13
Next let's delete our MockedView class and
these two lines that relied on it.
1:16
Then, let's add two lines above
our MainActivityView field
1:30
And type At Mach and that's it.
1:35
When you're using Mockito's,
all you need to do to mock a class.
1:40
Back in the edit text updated method,
1:45
let's see how we can handle
the assert step using my Mockito.
1:47
Let's type Mockito dot verify.
1:49
pass in our view variable and then,
1:59
add the change text of your text and
pass on our string.
2:03
Now let's run the test again and
we're still passing.
2:09
Great job!
2:15
Mocking classes ourselves
is not only a lot of work,
2:16
it's also a great way to
introduce errors into our tests.
2:19
Letting Mockito handle mocking frees us
up to focus on what's really important.
2:24
The tests!
2:29
Speaking of the tests, now that we've got
Mockito all set up, let's go ahead and
2:31
finish writing the rest of these tests.
2:35
Starting with color selected.
2:38
For this test, we want to validate
that passing in a certain
2:42
index to our presenter results
in the right color on our view.
2:45
Let's start by copying in
the Arrange-Act-Assert comments
2:50
from the teacher's notes below.
2:53
Then for the Arrange section,
let's create a new integer variable for
3:00
the index and set it equal to two.
3:05
Then let's create another
new integer variable,oops,
3:09
looks like I missed a tab here.
3:13
And let's name this new integer variable
3:18
givenColor and
set it equal to Color.GREEN.
3:23
Which if we check MainActivityPresenter
is the color at index
3:28
2.
3:33
Next, for the app section, let's call
3:38
presenter dot color selected and
pass in our index.
3:42
Finally,for the assert section,
3:47
let's type Mockito dot verify.
3:50
Pass on our view variable and
3:56
then add dot change background color and
pass in our given color.
3:58
Then let's comment out the Act section, so
we can see what a failing test looks like.
4:06
Then let's right click and
run this color selected test and
4:15
it looks like we missed a call.
4:20
To view, change background color with
a parameter of about negative 17 million.
4:23
Neat!
4:30
Now let's bring back the Act section.
4:31
And run the test again to see where we
stand and perfect, another passing test.
4:36
Moving on to the last test.
4:44
Let's copy and paste in the comments
4:46
And then, start with the arrange
step by declaring a new class
5:04
variable named clazz with two Z's.
5:09
To avoid using the class keyword and
let's set it equal to otherActivity.class.
5:14
Then for the Act step, let's call
5:21
presenter.launchOtherActivityButtonClic-
ked.
5:26
And pass on our new class variable.
5:33
Lastly for the Assert step, let's type
5:36
Mockito.verify, pass
on our view interface.
5:41
And then add type launchotherActivity and
pass on clazz.
5:46
To verify that this method was called
with our class variable as a parameter.
5:54
Now, for one final test.
6:00
Let's right-click on our test
class itself or just above it and
6:02
pick run MainActivityPresenterTest
to run all three tests at once.
6:09
And there,
we have it a fully functioning Android app
6:15
with fully functioning unit tests.
6:18
We've come a long way since
the start of this course.
6:21
From figuring out why we can't
use the Android framework.
6:24
To seeing how Mojito lets us make
concise unit tests by making mocking
6:27
something we really don't
need to worry about.
6:31
But there's more than one way
to test an android project.
6:34
In the next stage, we'll test this same
project again using Robo lectured.
6:37
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