[No Audio]
In this video we're going to create a brand
new project which uses JUnit and Mockito and we
can use this in the next few videos then
to demonstrate how to use Mockito itself.
So let's jump in.
So first off I'll create a new project.
[No Audio]
We've seen this before, but I'll just do ultra fast.
[No Audio]
Just give it a simple Groupid or Artifactid.
Go to next
[No Audio]
click Finish.
[No Audio]
Just set this to Enable Auto-Import.
Leap up here,
[No Audio]
adding the junit dependency.
[No Audio]
And underneath we're going to
add the Mockito dependency.
[No Audio]
So 3.3.0 is fine for now.
Just tidy that up.
[No Audio]
And if I go across into External Libraries,
we'll see here that we've got junit that
we just included, which is brought in hamcrest.
And we also have Mockito-core:3.3.0 and some
other dependencies it's brought in as well, which
are these bytebuddy ones here and objenesis, those are
basically responsible for doing a little bit of byte
code magic behind the scenes which allows Mockito
to do its thing and to be able to create these
mock objects, programmatically incurred as we'll see.
So anyway, so Mockito Core is the main library
that you want to bring in when you're writing
mock object based unit tests with Mockito.
And once you've done that you'll
be able to access its API. So let's do that.
Now let's just go into the
project structure into src, test, java.
We'll just put a package in here,
[No Audio]
com.javaeasily.
[No Audio]
demos.mockito.myapp
I'll just copy that into the clipboard, so I can create
a corresponding package in the production source
folder as well, which we'll use later when we write
the production classes we're going to test.
And now I'll just put a quick test class in here now,
just so we can see Mockito to check it set up okay.
[No Audio]
I'll just call it AppTest for now. That's fine.
So at this point now we should be able
to use Mockito and at least in buckets API.
Now let's just create a test method and just
try to create a mock object using Mockito just
to make sure that we've got the Mockito API
set up correctly and accessible to us.
So if we jump into here,
[No Audio]
just put a test method in, we'll just call it testIt.
Like I said, the test method is irrelevant
now we're going to delete this anyway.
We just want to make sure that
we've got Mockito set up okay.
And then we can access the API.
So let's just call into Mockito.
You can see here when we type Mockito
intelliJ ideas, give it a suggestion, which is
the class in the org.mockito package.
So org.mockito is the main package for
Mockito and the Mockito class itself is the
main entry point to the framework.
So if we just go Mockito.mock and we can basically
now pass it a class to mock
set calendar from java.util and
we have to give a class reference for that,
[No Audio]
and we get back something that, as far as the
compiler is aware is an instance
of the class time we passed him.
So just put mock calendar = Mockito.mock.
I just put the test annotation in here as well.
And now if we run that
[No Audio]
that's fine and we just put a Breakpoint here and
we'll just see what we've actually got.
So if we Debug this,
[No Audio]
then we can see here mockCalendar.
It's a calendar type but it's actually a
MockitaMock and at this stage we know
that we've set up Mockito correctly and we
can start creating those mock objects done here.
I will explore that further in the next videos.