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
With our DBUnit setup complete from the previous video, it's time to write tests that verify our DAOs are working as intended!
Sync Your Code to the Start of this Video
git checkout -f v10
Using Git with this Workshop
See the README on Github for information on how to use Git with this repo to follow along with this workshop.
With DB units set up for DAO tasks in
our test XML data in place for insertion
0:00
before the beginning of our DAO test,
we're ready to code some test methods.
0:05
First there's something we need to
take care of in our set up method, so
0:10
let's code that.
0:14
That's going to be annotated
with before public void setup.
0:15
Now each of our DAO methods that we're
testing contains a custom query that
0:21
injects authentication data.
0:24
That means our DAO under test is going to
need that authentication data available
0:26
for injection.
0:30
To get that started, let's instantiate the
user object that we'd like to impersonate
0:31
for our tests, also a user make sure
I'm choosing user in my application.
0:35
So User user = new User(); and
I'll use user one who has an ID of one.
0:42
So user.setId we'll say (1L)
in the long form then we need
0:48
to make sure spring security recognizes
this user as the one who authenticated.
0:55
One way we can do that is to set
the authentication object of the security
0:59
content, here's how we do that,
we use SecurityContextHolder
1:03
right here .getContext(
).setAuthentication( );.
1:09
And we will set a (new
UsernamePasswordAuthenticationToken
1:16
with this user, and we don't care so
1:21
much about the actual credentials,
so I will use null there.
1:23
Now let's write a couple tests.
1:29
Let's test the find all method for
user one.
1:33
So let's create a test for
that and I'll say public void.
1:36
We'll say that
findAll_ShouldReturnTwo favorites.
1:41
And as a second test, public void
1:51
we'll say that a save_ShouldPersistEntity,
1:56
throws Exception.
2:04
To facilitate our assertions, I'm going
to pull in a couple static imports,
2:06
you can use your IDE to do this or
you can type them yourselves.
2:11
So, import static,
I'm gonna use those ham crest matchers,
2:14
org.hamcrest.MatcherAssert.* and
2:22
then import static,
org.hamcrest.Matchers.*.
2:26
Okay, now back down to the first test, for
2:33
its implementation let's
assertThat(dao.findAll( ),
2:37
hasSize(2)); and let's code the test for
2:44
saving before running our test.
2:49
Here we'll create a favorite object and
set its place ID.
2:53
Now I'm going to use that favorite
builder, so Favorite fave
2:56
= new_Favorite.FavoriteBuilder.
3:01
And then you can say withPlaceId and
3:08
then just make one up, I'll say
treehouse and it doesn't really matter.
3:10
OK cool and then,
don't forget when you're using a builder,
3:15
don't forget to build that thing.
3:19
Okay, if you want to
remove this right here,
3:21
which is always nice,
you can do a static import of that.
3:23
So I can go up here, import static
3:27
com.teamtreehouse.domain.Favorite.Favorit-
eBuilder.
3:31
Cool, that whole import statement
to save that little bit of typing.
3:38
Well I suppose it's up for
a debate whether or not that was worth it.
3:42
Okay, so cool,
I've got my favorite object nonetheless.
3:45
So let's use the DAO that is
auto-wired up here to save this entity.
3:50
So I'll say dao.saveForCurrentUser and
3:54
then I'll save that fave,
so happy that rhymed.
3:59
Now in order to ensure that
the entity was indeed persisted,
4:03
let's make sure that the find-by-place ID
method returns a non null favorite object.
4:06
So here I will assert that
4:10
the dao.findByPlaceID using
the same place ID here.
4:14
Now since I'm copying and pasting this
text, I should probably create a string
4:19
variable for it so I don't end up with
a failing test simply because I mistyped.
4:23
So it would be nice to do something
like this and like this, placeId.
4:28
Okay, let's make that possible, placeId,
and let's paste that right there.
4:34
Cool, let's make sure
the dao.findPlaceId is not null,
4:40
notNullValue and
it is an instance of Favorite.class.
4:48
Okay,sSo we want to make sure that when we
call the DAO find by place ID passing it
4:54
the same ID that what we get back
is a non null favorite object.
4:59
Just another thing that's available
to you in this hamcrest library.
5:04
Okay, now for one final time in this
workshop, let's run these tests.
5:12
Ctrl+Shift+R.
5:16
And here you'll see a full
application context being created,
5:21
which is why you see that Spring Boot logo
show up top and look at that, green again!
5:25
Now as I said earlier,
the test grass aint always this green,
5:31
you might get some errors upon running
your test that you don't understand.
5:34
If there are errors in running tests,
5:38
be sure to scroll through all that output
to get the root of any exceptions that
5:40
might be occurring when the spring test
framework attempts to run your tests.
5:45
These exceptions can be the fuel for
5:48
online searches in pursuit of answers
to all your testing questions.
5:50
You need to sign up for Treehouse in order to download course files.
Sign up