[No Audio]
In this video, we're going to look at JUnits Assertions
API, which it gives us to get a bit of a feel for the
kinds of assertions which we can do in our code.
So let's jump in. So
picking up from where we were in the
last video, if we just get rid of that now,
since we're talking about assertions, I should
dig into these a little bit further.
So JUnit has an assert class.
So if I type Assert and then ., you
can see here there are a ton of different methods
which enable you to assert different things, courtesy
of JUnits Assertions API.
So in general you can assert things, but you
can also fail tests like we've seen before.
But focusing on the assertion side, you
can assert that things are not null.
So in other words, like an object reference
is present in a variable. So it's useful
for example, if you call a method and
you expect an object to be returned, you
might want to check that it's not null.
You can check that some boolean condition is true.
You can check that two arrays are equal. You can
see here they're overloaded for different types
[No Audio]
pointing the two objects are equal, and again
they are overloaded methods, so you can feel
free to check those out in your own time.
But just note that in general, these methods tend to
come in pairs, so you can have assertEquals,
and then there'll be an assertNotEquals, or there's
an assertTrue, then there's an assertFalse.
And broadly, what these allow you to check
for, are basically (not clear) of object references.
Equality of objects and values. Actually
speaking of equality as well, you'll notice here that
we have the first argument is the expected thing that
we expect, and the second argument is the actual object
that we get back, which we'll see in a second.
We also have assertions of as many equal
to and that kind of thing.
So the best thing to do now is to look
into your project, which you have set up in the
previous section, and just do what I did. Just do
Assert. after you've imported the org.junit.assert
class and just go through those methods and really
take them in and have a bit of a play around and
get a feel for how they work.
And in this way you get to know them
first hand as you get to use them.