1 00:00:00,000 --> 00:00:02,350 [No Audio] 2 00:00:02,350 --> 00:00:04,300 In this video, we're going to explore how we can 3 00:00:04,300 --> 00:00:07,433 analyze the production code that we're testing and use 4 00:00:07,433 --> 00:00:10,366 that to drive how we set up the arrange block in our 5 00:00:10,366 --> 00:00:13,682 tests. So let's jump back in now to writing the test back 6 00:00:13,682 --> 00:00:16,903 to the Arrange/ Act/ Assert template we saw earlier. 7 00:00:16,903 --> 00:00:20,449 So again, we've got arrange, act 8 00:00:20,449 --> 00:00:22,419 [No Audio] 9 00:00:22,419 --> 00:00:24,303 assert. Always nice to get into those habits. 10 00:00:24,303 --> 00:00:26,369 [No Audio] 11 00:00:26,369 --> 00:00:29,240 Let's just copy it here as well for the AdminUsers1. 12 00:00:29,240 --> 00:00:31,796 Now we know where we're putting the code and what 13 00:00:31,796 --> 00:00:33,478 the calls are going to be in those sections. 14 00:00:33,478 --> 00:00:35,756 So in arrange, we're really setting up the 15 00:00:35,756 --> 00:00:37,772 mocks to act as we want them to. 16 00:00:37,772 --> 00:00:42,304 So here it's going to be Mockito.when 17 00:00:42,304 --> 00:00:45,746 [No Audio] 18 00:00:45,746 --> 00:00:48,270 make that a static import. 19 00:00:48,270 --> 00:00:53,508 So when(userRepository.findAll()), let's just 20 00:00:53,508 --> 00:00:55,668 look back as to why we're doing findAll. 21 00:00:55,668 --> 00:00:58,044 So if we go back to the production class, 22 00:00:58,044 --> 00:01:00,588 we're going to test this getRegularUsers method yeah. 23 00:01:00,588 --> 00:01:04,272 And so getRegularUsers calls into this other 24 00:01:04,272 --> 00:01:08,493 method here, which is getUsersByUserType. 25 00:01:08,493 --> 00:01:13,268 And that method calls UserRepository.findAll. 26 00:01:13,268 --> 00:01:15,494 So that's why we're stubbing that particular method. 27 00:01:15,494 --> 00:01:18,774 And the purpose of the regular users test then 28 00:01:18,774 --> 00:01:22,092 is really to test this bit of code here. 29 00:01:22,092 --> 00:01:23,583 And there's a few things we could do with this. 30 00:01:23,583 --> 00:01:26,219 We could, for example, set the userRepository 31 00:01:26,219 --> 00:01:28,569 up to return no objects whatsoever. 32 00:01:28,569 --> 00:01:31,366 So like an empty list or even a 33 00:01:31,368 --> 00:01:34,382 null list, possibly and to throw an exception. 34 00:01:34,382 --> 00:01:36,732 That's something else it could do to return a list 35 00:01:36,732 --> 00:01:39,936 of users who are all live and regular users, a 36 00:01:39,936 --> 00:01:42,216 list of users who are a mix of live and 37 00:01:42,216 --> 00:01:45,660 RegularUsers and AdminUsers and that kind of thing. 38 00:01:45,660 --> 00:01:48,170 Maybe even a list of, say, a few RegularUser 39 00:01:48,170 --> 00:01:49,896 and then a user which doesn't have 40 00:01:49,896 --> 00:01:51,554 a username or doesn't have a password. 41 00:01:51,554 --> 00:01:53,676 So you can really get into setting up the 42 00:01:53,676 --> 00:01:59,487 test data at a really fine grained level, to 43 00:01:59,487 --> 00:01:59,487 really test how robust this method really is. 44 00:01:59,487 --> 00:02:01,596 Certainly at first sight, it looks like a 45 00:02:01,596 --> 00:02:03,456 reasonable enough method, but the chances are you 46 00:02:03,456 --> 00:02:05,148 can always find issues with it. 47 00:02:05,148 --> 00:02:06,516 It just depends how deep you want to 48 00:02:06,516 --> 00:02:09,864 go and balancing that up, the implementation cost 49 00:02:09,864 --> 00:02:12,443 of fixing up those kinds of issues, that 50 00:02:12,443 --> 00:02:15,720 is, with the likelihood of those issues actually 51 00:02:15,720 --> 00:02:18,266 manifesting themselves or occurring in production. 52 00:02:18,266 --> 00:02:20,916 But nevertheless, using unit testing is a nice way 53 00:02:20,916 --> 00:02:22,236 to get to all these kind of issues. 54 00:02:22,236 --> 00:02:23,256 And that's really what you want to do, 55 00:02:23,256 --> 00:02:25,404 you want to basically write unit tests so 56 00:02:25,404 --> 00:02:27,828 that you know that your code can function 57 00:02:27,828 --> 00:02:29,580 in as robust a way as possible. 58 00:02:29,580 --> 00:02:31,663 That's really what it's all about. 59 00:02:31,663 --> 00:02:35,134 So I go back to UserLookupServiceTest. 60 00:02:35,134 --> 00:02:38,376 So we're saying when we findAll users and 61 00:02:38,376 --> 00:02:39,459 useRepository, 62 00:02:39,459 --> 00:02:41,534 [No Audio] 63 00:02:41,534 --> 00:02:46,530 then I'm going to theReturn(userList). 64 00:02:46,530 --> 00:02:49,996 And let's just create use list now, to list of user 65 00:02:49,996 --> 00:02:55,513 userList new LinkedList 66 00:02:55,513 --> 00:02:57,987 [No Audio] 67 00:02:57,987 --> 00:02:59,887 import User class 68 00:02:59,887 --> 00:03:01,970 [No Audio] 69 00:03:01,970 --> 00:03:03,170 import the list class 70 00:03:03,170 --> 00:03:05,345 [No Audio] 71 00:03:05,345 --> 00:03:07,862 UserList.add 72 00:03:07,862 --> 00:03:10,226 we've got that static factory method. 73 00:03:10,226 --> 00:03:13,406 Remember so createRegularUser. So anne. 74 00:03:13,406 --> 00:03:16,256 [No Audio] 75 00:03:16,256 --> 00:03:17,673 abc123 76 00:03:17,673 --> 00:03:19,634 [No Audio] 77 00:03:19,634 --> 00:03:21,630 And let's createRegularUser. 78 00:03:21,630 --> 00:03:25,029 Another RegularUser. donald 79 00:03:25,029 --> 00:03:28,487 [No Audio] 80 00:03:28,487 --> 00:03:30,905 dbc321 81 00:03:30,905 --> 00:03:32,321 and maybe 82 00:03:32,321 --> 00:03:40,030 an admin user. 83 00:03:40,030 --> 00:03:42,287 Interesting name for AdminUser. 84 00:03:42,287 --> 00:03:45,230 pwd098 85 00:03:45,230 --> 00:03:47,516 Now this is good we've got two RegularUsers which 86 00:03:47,516 --> 00:03:50,914 are in there an AdminUser and the userRepository 87 00:03:50,914 --> 00:03:53,312 mock is set up to return that data set return 88 00:03:53,312 --> 00:03:55,954 that list of users when it's invoked so that arrange 89 00:03:55,954 --> 00:03:58,312 section looks pretty good to me at this stage.