[No Audio]
So Mockito is a library that you can include
and use in the unit tests of your Java
projects to enable you to create various types of
so called testables which your tests can use.
Now there are different types of testables, and you'll
hear words like dummy, stub, mock, spy, and fake.
And don't worry, we'll cover what these are later
in the course, but for now we're just going
to refer to them as mock objects in this
little intro video to keep things nice and simple.
So these Mock objects which Mockito lets you create.
You can think of these as stand-ins for real objects
which you can use in your tests. When you write test
code, specifically when you write a unit test, you're
really interested in testing the unit that's the
class you're testing and nothing else.
That means that in your unit test, the only real
production code which executes should be this class
you're writing the test for, and that class will
work with the standings these mock objects, as it
would do in the real world.
This is where Mockito comes in.
It lets us create these mock objects super easily and
lets us set up how they behave in our tests.
Doing this allows you to write really good
unit tests which can test your application much
more comprehensively and quicker than before.
Let's think about a web app for a second.
It might have a LoginController which can take the
username and password entered by the user on a login
screen and return a view of the homepage for logged in
user if the password is good, or return the user back
to the login screen if the password is bad.
So here's our LoginController, waiting for those
username and password combinations to come in.
Now, to do its job, you will probably
need to delegate to some service. So let's say
it has a reference to an authentication service.
In reality, they'd probably be a
repository too, to access the database.
But let's leave that aside for now.
The point is that in production, when the app
is deployed and running, these two classes work
together to get the user logged in or kicking out
with the login controller processing the web request,
but delegating to the authentication service to do the
actual real work of verifying the user's credentials.
So thinking about the testing aspect, when I want to
test the LoginController, when I want to write a
test for it, a unit test for it. What I want to have is
the real LoginController itself.
It's what I'm testing, so it needs to be the real deal.
But I want to have a mock
Authentication Service that it works with.
That's because the scope of my
unit test is just the unit.
It's just the thing I'm testing, which
in this case it's a LoginController.
So I don't want anything else to be in
that test that's real, only the controller.
But my controller still needs a service to work with.
So I create a mock Authentication Service that
is a stand in for the real thing.
Now, having a mock object courtesy
of Mockito in place is awesome.
It means I can put the real code through its paces.
In other words, test all sorts of scenarios
to see how my production code will behave.
Maybe in the first test, I set up
the mock service to behave nicely and return
that the user's password is correct.
Maybe in the second test I set up the mock
to misbehave and throw an exception and in the third
test I might return an empty user object.
And there are other tests too which I could write.
The point is that I can see how my production
code behaves through different scenarios in a way
that's quick to set up, develop and fun to use.
And here I might even fix up the production code
while I'm at it, as I learned more about my
code than I initially thought about when I was writing
it and I'll have time to do this since Mockito
is quick to use and code with.
So this is what Mockito is all about.
It's about helping you write better unit tests through
offering a nice and easy to use API for creating,
manipulating and even validating these mock objects.
Specifically, it's easy to set up and install offers an
easy way to create different types of test doubles
those mock objects we've just seen, as an easy to use
API for stubbing behavior that's programming the mocks
to do what you want them to do in your tests.
It's also got an API for verifying interactions on
mocks two which lets you check that the collaborators
of your production class received the right calls with
the right parameters and return the right responses.
And finally it's got a noninvasive API so
it's easy to apply to real world projects.
So there it is.
You now have a high level overview of Mockito
and I'll see you in the next video to
show you exactly how we can use it.