1 00:00:00,000 --> 00:00:02,170 [No Audio] 2 00:00:02,170 --> 00:00:03,536 So in this video we're going to see how 3 00:00:03,536 --> 00:00:05,396 we can replace the equalTo matches which we 4 00:00:05,396 --> 00:00:07,364 had before, and we're going to replace them by 5 00:00:07,364 --> 00:00:09,830 demonstrating how we can compose matches together 6 00:00:09,830 --> 00:00:11,914 using the all of matches from Hamcrest. 7 00:00:11,914 --> 00:00:13,774 And also we're going to take a look at property matches 8 00:00:13,774 --> 00:00:16,064 as well, which will allow us to to remove the call 9 00:00:16,064 --> 00:00:18,730 to that factory method to create the user objects. 10 00:00:18,730 --> 00:00:20,418 So let's jump in. 11 00:00:20,418 --> 00:00:22,892 So let's replace this one here. 12 00:00:22,892 --> 00:00:25,496 First of all, let's write this in a different way. 13 00:00:25,496 --> 00:00:28,196 So the essence of this, assertion is basically that 14 00:00:28,196 --> 00:00:30,644 we've got a user object whose user name is 15 00:00:30,644 --> 00:00:33,788 "anne" and whose password is "abc123". 16 00:00:33,788 --> 00:00:35,830 So in other words, there are two properties 17 00:00:35,830 --> 00:00:38,768 here, a username property and a password property, 18 00:00:38,768 --> 00:00:41,854 assuming user sticks to Java being naming convention. 19 00:00:41,854 --> 00:00:44,553 So like a Podia where you have like a get username 20 00:00:44,553 --> 00:00:46,712 property and I get password property, which it does. 21 00:00:46,712 --> 00:00:49,128 If we just quickly pop into here now we can see 22 00:00:49,128 --> 00:00:52,661 inside user, we've got private String username, 23 00:00:52,661 --> 00:00:54,342 private String password, and then 24 00:00:54,356 --> 00:00:56,384 we've got corresponding getters for this. 25 00:00:56,384 --> 00:01:00,384 So we've got a getUsername and a getPassword here. 26 00:01:00,384 --> 00:01:02,192 So that's good. 27 00:01:02,192 --> 00:01:04,934 So because of this, we can change this 28 00:01:04,934 --> 00:01:09,870 [No Audio] 29 00:01:09,870 --> 00:01:13,600 to use a composing matcher called allOf. 30 00:01:13,600 --> 00:01:16,812 And if we see the signature for this, this 31 00:01:16,812 --> 00:01:18,852 is a matcher which takes a set of different 32 00:01:18,852 --> 00:01:21,534 matches and the semantics of this other each 33 00:01:21,534 --> 00:01:24,314 matcher that you supply to all of needs to evaluate 34 00:01:24,314 --> 00:01:27,550 to true for the all off matcher to evaluate to true. 35 00:01:27,550 --> 00:01:29,700 So we'll see what this means in a second. 36 00:01:29,700 --> 00:01:32,610 It'll become clearer as we cut the example. 37 00:01:32,610 --> 00:01:35,499 We can go for matches 38 00:01:35,499 --> 00:01:37,082 has property 39 00:01:37,082 --> 00:01:40,315 [No Audio] 40 00:01:40,315 --> 00:01:42,796 and it's the username property we're going to look at. 41 00:01:42,796 --> 00:01:45,604 And we're now passing a matcher, which is going to 42 00:01:45,604 --> 00:01:46,770 be equalTo 43 00:01:46,770 --> 00:01:48,887 [No Audio] 44 00:01:48,887 --> 00:01:49,837 "anne" 45 00:01:49,837 --> 00:01:52,062 [No Audio] 46 00:01:52,062 --> 00:01:54,224 let's do the import, static import for that. 47 00:01:54,224 --> 00:01:56,682 And the other thing, the other property we 48 00:01:56,696 --> 00:02:00,540 need to test is the password property. 49 00:02:00,540 --> 00:02:05,157 So password needs to do "abc123". 50 00:02:05,157 --> 00:02:08,865 So now if rerun this with control R. 51 00:02:08,865 --> 00:02:11,527 It should still pass, the test still pass. 52 00:02:11,527 --> 00:02:14,096 But as you can see we've done it in a different way. 53 00:02:14,096 --> 00:02:16,196 Instead of an equal to matcher which is 54 00:02:16,196 --> 00:02:20,314 an equa comparison on two objects, instead we're 55 00:02:20,314 --> 00:02:24,104 using a composition of has property matches which 56 00:02:24,104 --> 00:02:26,162 match the properties on the object. 57 00:02:26,162 --> 00:02:27,908 And because we're using allOf it's, going 58 00:02:27,908 --> 00:02:29,972 to evaluate all of those at the same 59 00:02:29,972 --> 00:02:32,307 time to ensure they're all true. 60 00:02:32,307 --> 00:02:33,656 So we can do the same and 61 00:02:33,656 --> 00:02:36,991 substitute this "donald" "dbc321". 62 00:02:36,991 --> 00:02:38,357 So let's just do this 63 00:02:38,357 --> 00:02:45,124 [No Audio] 64 00:02:45,124 --> 00:02:46,107 "donald" " 65 00:02:46,107 --> 00:02:48,582 [No Audio] 66 00:02:48,582 --> 00:02:53,572 dbc321" and run that and it runs again. 67 00:02:53,572 --> 00:02:55,744 So this is much nicer because it also 68 00:02:55,744 --> 00:02:58,198 enables us to do more fine grain testing. 69 00:02:58,198 --> 00:03:01,554 So whereas before, when we saw the equal to matcher 70 00:03:01,554 --> 00:03:04,353 which takes the expected and the actual object 71 00:03:04,353 --> 00:03:07,087 instances that equal to matchaer does a full equals 72 00:03:07,087 --> 00:03:09,020 comparison with both those objects, 73 00:03:09,020 --> 00:03:12,003 which means that every single field in those objects 74 00:03:12,003 --> 00:03:15,330 needs to be equal in accordance with the equals method 75 00:03:15,330 --> 00:03:16,890 otherwise the match wouldn't evaluate to 76 00:03:16,890 --> 00:03:19,804 true this is nicer because we can actually 77 00:03:19,804 --> 00:03:22,468 be more fine grained about things. So what I 78 00:03:22,468 --> 00:03:24,484 mean by that is we're not taking into 79 00:03:24,484 --> 00:03:26,884 account all of the different properties in here. 80 00:03:26,884 --> 00:03:28,850 We're just interested in username and password 81 00:03:28,850 --> 00:03:32,800 so you'll know that on the user class, we have extra 82 00:03:32,800 --> 00:03:36,075 fields so we've got this boolean live we've also got a 83 00:03:36,075 --> 00:03:39,375 userType as well, but because we're using these 84 00:03:39,375 --> 00:03:42,567 composed matches we can arbitrarily be as fine or 85 00:03:42,567 --> 00:03:45,117 costgrained as we want, without being at the mercy of 86 00:03:45,117 --> 00:03:47,284 the evaluation of the equals method between the two 87 00:03:47,284 --> 00:03:49,684 objects. You can really see that Hamcrest gives you a 88 00:03:49,684 --> 00:03:52,350 really powerful way to be able to do assertions and 89 00:03:52,350 --> 00:03:53,917 there are lots more matches as well in the library 90 00:03:53,917 --> 00:03:56,056 itself. I encourage you to have a look at those in 91 00:03:56,056 --> 00:03:58,288 the documentation with the library but this gave you a 92 00:03:58,288 --> 00:04:00,448 good look at the matches in action and how you 93 00:04:00,448 --> 00:04:02,836 can compose them and you can really see now how 94 00:04:02,836 --> 00:04:05,719 much of a powerful assertions library Hamcrest really 95 00:04:05,719 --> 00:04:08,322 is. And the more you go on in your unit testing 96 00:04:08,346 --> 00:04:10,004 journey the more you're going to learn about 97 00:04:10,004 --> 00:04:11,776 Hamcrest the more you're going to use it and the 98 00:04:11,776 --> 00:04:13,329 nice your tests are going to become. 99 00:04:13,329 --> 00:04:16,728 So yeah Hamcrest awesome assertions library use it 100 00:04:16,728 --> 00:04:18,296 wherever you can is much better than 101 00:04:18,296 --> 00:04:21,064 the Junit assertions API, but just make 102 00:04:21,064 --> 00:04:24,208 sure you swap in the full library as opposed to the 103 00:04:24,208 --> 00:04:26,245 stripped down version which JUnit it gets you out of 104 00:04:26,248 --> 00:04:29,262 the box but there it is a good overview of Hamcrest 105 00:04:29,286 --> 00:04:32,235 matches and Hamcrest assersions library in action. 106 00:04:32,235 --> 00:04:35,500 [No Audio]