[No Audio]
So in this video we're going to see how
we can replace the equalTo matches which we
had before, and we're going to replace them by
demonstrating how we can compose matches together
using the all of matches from Hamcrest.
And also we're going to take a look at property matches
as well, which will allow us to to remove the call
to that factory method to create the user objects.
So let's jump in.
So let's replace this one here.
First of all, let's write this in a different way.
So the essence of this, assertion is basically that
we've got a user object whose user name is
"anne" and whose password is "abc123".
So in other words, there are two properties
here, a username property and a password property,
assuming user sticks to Java being naming convention.
So like a Podia where you have like a get username
property and I get password property, which it does.
If we just quickly pop into here now we can see
inside user, we've got private String username,
private String password, and then
we've got corresponding getters for this.
So we've got a getUsername and a getPassword here.
So that's good.
So because of this, we can change this
[No Audio]
to use a composing matcher called allOf.
And if we see the signature for this, this
is a matcher which takes a set of different
matches and the semantics of this other each
matcher that you supply to all of needs to evaluate
to true for the all off matcher to evaluate to true.
So we'll see what this means in a second.
It'll become clearer as we cut the example.
We can go for matches
has property
[No Audio]
and it's the username property we're going to look at.
And we're now passing a matcher, which is going to
be equalTo
[No Audio]
"anne"
[No Audio]
let's do the import, static import for that.
And the other thing, the other property we
need to test is the password property.
So password needs to do "abc123".
So now if rerun this with control R.
It should still pass, the test still pass.
But as you can see we've done it in a different way.
Instead of an equal to matcher which is
an equa comparison on two objects, instead we're
using a composition of has property matches which
match the properties on the object.
And because we're using allOf it's, going
to evaluate all of those at the same
time to ensure they're all true.
So we can do the same and
substitute this "donald" "dbc321".
So let's just do this
[No Audio]
"donald" "
[No Audio]
dbc321" and run that and it runs again.
So this is much nicer because it also
enables us to do more fine grain testing.
So whereas before, when we saw the equal to matcher
which takes the expected and the actual object
instances that equal to matchaer does a full equals
comparison with both those objects,
which means that every single field in those objects
needs to be equal in accordance with the equals method
otherwise the match wouldn't evaluate to
true this is nicer because we can actually
be more fine grained about things. So what I
mean by that is we're not taking into
account all of the different properties in here.
We're just interested in username and password
so you'll know that on the user class, we have extra
fields so we've got this boolean live we've also got a
userType as well, but because we're using these
composed matches we can arbitrarily be as fine or
costgrained as we want, without being at the mercy of
the evaluation of the equals method between the two
objects. You can really see that Hamcrest gives you a
really powerful way to be able to do assertions and
there are lots more matches as well in the library
itself. I encourage you to have a look at those in
the documentation with the library but this gave you a
good look at the matches in action and how you
can compose them and you can really see now how
much of a powerful assertions library Hamcrest really
is. And the more you go on in your unit testing
journey the more you're going to learn about
Hamcrest the more you're going to use it and the
nice your tests are going to become.
So yeah Hamcrest awesome assertions library use it
wherever you can is much better than
the Junit assertions API, but just make
sure you swap in the full library as opposed to the
stripped down version which JUnit it gets you out of
the box but there it is a good overview of Hamcrest
matches and Hamcrest assersions library in action.
[No Audio]