1 00:00:00,300 --> 00:00:03,270 Unit test best practices have evolved overtime. 2 00:00:03,270 --> 00:00:06,870 The test community at large has embraced several acronyms 3 00:00:06,870 --> 00:00:09,390 that help promote the writing of good tests. 4 00:00:09,390 --> 00:00:11,520 This first one you'll hear a lot, and 5 00:00:11,520 --> 00:00:14,570 it's used to help you remember how to lay out your tests. 6 00:00:14,570 --> 00:00:16,170 AAA is this. 7 00:00:16,170 --> 00:00:20,520 Arrange, in most cases you'll need to get things all set up before the test. 8 00:00:20,520 --> 00:00:22,970 To get things in the right state before the test 9 00:00:22,970 --> 00:00:26,550 you need to arrange your objects so they're ready to be tested. 10 00:00:26,550 --> 00:00:30,900 Act, in the act portion of test you cause the behavior that your testing to happen, 11 00:00:30,900 --> 00:00:32,820 whatever that action might be. 12 00:00:32,820 --> 00:00:37,570 Assert, in the assert portion you verify that the expected behavior occurred. 13 00:00:38,760 --> 00:00:40,898 Let's go write some tests using the AAA style. 14 00:00:41,990 --> 00:00:44,500 Okay, so let's work through the tests here. 15 00:00:44,500 --> 00:00:47,680 This adding funds, increments available funds. 16 00:00:47,680 --> 00:00:50,230 Well first actually, let's get rid of this original test that we have here, 17 00:00:50,230 --> 00:00:52,370 this failer, let's get rid of that. 18 00:00:52,370 --> 00:00:55,232 So, first things first, let's arrange things. 19 00:00:55,232 --> 00:00:58,472 So in the arranging of this we need a creditor object. 20 00:00:58,472 --> 00:00:59,172 Right? 21 00:00:59,172 --> 00:01:01,320 So let's do that, let's make a creditor. 22 00:01:03,820 --> 00:01:11,291 Creditor = new Creditor. 23 00:01:11,291 --> 00:01:14,411 Okay, so that's basically all we need to do in the arrange phase. 24 00:01:14,411 --> 00:01:15,111 Right? 25 00:01:15,111 --> 00:01:18,760 Now sometimes this is all the arrange portion needs. 26 00:01:18,760 --> 00:01:21,120 It's just simple object creation. 27 00:01:21,120 --> 00:01:24,750 Note also how even though the tests are in a different folder 28 00:01:24,750 --> 00:01:27,550 they're still in the same package as the creditor class. 29 00:01:27,550 --> 00:01:29,560 See how we didn't import the creditor object at all? 30 00:01:29,560 --> 00:01:30,370 We didn't. 31 00:01:30,370 --> 00:01:33,860 It's because they're different folders but they're in the same package. 32 00:01:33,860 --> 00:01:37,410 A JUnit style that has emerged is to separate your AAA 33 00:01:37,410 --> 00:01:40,950 methods by using a blank line between each portion. 34 00:01:40,950 --> 00:01:45,440 So let's do that by starting our second A, Act. 35 00:01:45,440 --> 00:01:47,640 So let's add a quarter. 36 00:01:47,640 --> 00:01:52,890 Now this method addFunds takes an integer, 37 00:01:52,890 --> 00:01:57,470 I use sense just in case we wanted to eventually extend this past U.S. currency. 38 00:01:59,180 --> 00:02:02,070 Okay, and let's go ahead and we'll add another quarter. 39 00:02:02,070 --> 00:02:03,271 25 cents. 40 00:02:03,271 --> 00:02:03,771 Right? 41 00:02:08,591 --> 00:02:10,831 And now, our final, Assert. 42 00:02:10,831 --> 00:02:14,631 So what we wanna do is make sure that there is 50 cents in there. 43 00:02:14,631 --> 00:02:15,390 Right? 44 00:02:15,390 --> 00:02:17,270 So, let's just do that. 45 00:02:17,270 --> 00:02:21,890 In the case that this is new to you, the verb assert in this case, 46 00:02:21,890 --> 00:02:23,900 means to verify a fact. 47 00:02:23,900 --> 00:02:27,460 So remember that the JUnit assert class, has a ton of static methods for 48 00:02:27,460 --> 00:02:30,440 checking things that we statically imported all of them. 49 00:02:30,440 --> 00:02:32,860 We imported that fail before, remember? 50 00:02:32,860 --> 00:02:34,761 So let's do the same thing with the one that we're looking for. 51 00:02:34,761 --> 00:02:38,701 So we want to assert that the available funds are 50. 52 00:02:38,701 --> 00:02:39,760 Right? 53 00:02:39,760 --> 00:02:44,380 So the method that we want is assert, you can see there's a whole bunch here, 54 00:02:44,380 --> 00:02:45,740 equals. 55 00:02:45,740 --> 00:02:48,690 And now the thing that gets most people confused when they 56 00:02:48,690 --> 00:02:50,590 come to this framework is this. 57 00:02:50,590 --> 00:02:57,180 All of the assert methods parameter order is [FOREIGN] it is very important. 58 00:02:57,180 --> 00:03:02,190 Okay, first is the expected value and then it's the actual value. 59 00:03:02,190 --> 00:03:03,550 Here look, IntelliJ reminds us. 60 00:03:03,550 --> 00:03:05,390 It's expected and then it's actual. 61 00:03:06,790 --> 00:03:08,700 There's an optional message you can conclude, 62 00:03:08,700 --> 00:03:09,810 but this is the one that we want. 63 00:03:09,810 --> 00:03:13,150 See, it's expected and then actual. 64 00:03:13,150 --> 00:03:15,150 So we want the expected to be 50, 65 00:03:15,150 --> 00:03:20,390 and then the actual value is whatever is in available funds. 66 00:03:20,390 --> 00:03:21,770 Awesome, now we're gonna run it. 67 00:03:23,490 --> 00:03:25,231 Bam, it passes. It feels good. 68 00:03:25,231 --> 00:03:25,851 Right? 69 00:03:25,851 --> 00:03:30,762 Now, one thing I wanna stress here is that you always find the right assert method. 70 00:03:30,762 --> 00:03:33,540 You wanna do this because it produces awesome error messages. 71 00:03:33,540 --> 00:03:34,701 Watch this, let's pop out that second quarter. 72 00:03:34,701 --> 00:03:36,380 Let's just go ahead and comment this line. 73 00:03:36,380 --> 00:03:38,580 And then let's run it again. 74 00:03:38,580 --> 00:03:39,080 Get. 75 00:03:40,786 --> 00:03:44,175 And see how it says expected 50 and actual is 25. 76 00:03:44,175 --> 00:03:44,736 That's pretty awesome. 77 00:03:44,736 --> 00:03:46,716 That's showing the difference there right away. 78 00:03:46,716 --> 00:03:49,355 You can look at that and see what the problem is. 79 00:03:49,355 --> 00:03:52,175 So there we go, the AAA in action. 80 00:03:52,175 --> 00:03:56,550 Now, for those of you who are expecting a 50 Cent joke like the rapper, 81 00:03:56,550 --> 00:04:00,231 this test ensures that we will get rich or it will die trying. 82 00:04:00,231 --> 00:04:02,771 Okay, so let's practice another one. 83 00:04:04,911 --> 00:04:08,920 Let's look at the code over here in creditor. 84 00:04:08,920 --> 00:04:11,050 And let's see. 85 00:04:11,050 --> 00:04:13,390 Okay, refund does actually look pretty interesting here. 86 00:04:14,730 --> 00:04:18,340 So, let's make sure that the refund is all of the money and 87 00:04:18,340 --> 00:04:21,040 that it empties out the available funds. 88 00:04:21,040 --> 00:04:25,766 Okay, so we'll go ahead and we'll Go To 89 00:04:25,766 --> 00:04:29,690 > Test, and again that's Shift+Cmd+T on a Mac, 90 00:04:29,690 --> 00:04:33,200 and we're gonna choose CreditorTest, and we'll pop over there. 91 00:04:33,200 --> 00:04:37,060 So that's, if you don't wanna make a new one, you can jump back over that way. 92 00:04:37,060 --> 00:04:40,310 All right, and we'll generate a new test method. 93 00:04:40,310 --> 00:04:44,514 And lets name this one 94 00:04:44,514 --> 00:04:52,696 refundingReturnsAllAvailableFunds. 95 00:04:52,696 --> 00:04:54,616 Okay, so first we'll arrange. 96 00:04:54,616 --> 00:04:55,256 Right? 97 00:04:55,256 --> 00:04:58,852 So we'll need a creditor and in this case we're also 98 00:04:58,852 --> 00:05:03,442 gonna arrange the fact that there's already some funds in there. 99 00:05:03,442 --> 00:05:07,115 So we'll say creditor.addFunds, and 100 00:05:07,115 --> 00:05:13,063 we'll just put a dime in there, ten cents, and then we'll act. 101 00:05:13,063 --> 00:05:17,463 Okay, so we'll get the results back from the refund method. 102 00:05:17,463 --> 00:05:21,122 Right? 103 00:05:22,142 --> 00:05:27,125 And then we'll assert, and so again, this one we're gonna assert 104 00:05:27,125 --> 00:05:32,130 that we expect 10 to be stored in the variable refund. 105 00:05:32,130 --> 00:05:34,390 Right? We would expect our refund to be ten. 106 00:05:34,390 --> 00:05:36,450 Refunding returns all available funds. 107 00:05:36,450 --> 00:05:37,510 Perfect. 108 00:05:37,510 --> 00:05:39,330 Hey, I got an idea. 109 00:05:39,330 --> 00:05:40,285 While we're in here, 110 00:05:40,285 --> 00:05:43,011 why don't we also make sure that the available funds are empty. 111 00:05:43,011 --> 00:05:47,782 So let's say assertEquals(0 112 00:05:47,782 --> 00:05:53,263 creditor.getAvailableFunds). 113 00:05:53,263 --> 00:05:57,568 All right, and we'll run that. 114 00:05:57,568 --> 00:05:59,120 Beautiful. 115 00:05:59,120 --> 00:06:00,560 Wait a second. 116 00:06:00,560 --> 00:06:04,550 Does that test name really define what is happening? 117 00:06:04,550 --> 00:06:06,749 What if that second assert fails? 118 00:06:08,490 --> 00:06:10,080 It wouldn't make, really, sense, would it? 119 00:06:10,080 --> 00:06:13,430 When it failed, it would say refunding returns all available funds, but 120 00:06:13,430 --> 00:06:14,301 that's not true. 121 00:06:14,301 --> 00:06:15,801 That's not what we're testing there. 122 00:06:15,801 --> 00:06:17,310 Right? 123 00:06:17,310 --> 00:06:20,940 So, what I did here was intentionally break another best practice, 124 00:06:20,940 --> 00:06:21,780 just so you could see it. 125 00:06:21,780 --> 00:06:23,310 It's super common. 126 00:06:23,310 --> 00:06:26,660 You should really shoot for a single assertion in each test. 127 00:06:26,660 --> 00:06:29,350 Those assertion errors work much like exceptions. 128 00:06:29,350 --> 00:06:33,660 When one is encountered, they immediately exit and the rest of the code doesn't run. 129 00:06:33,660 --> 00:06:36,200 So it's possible that both of those are false and 130 00:06:36,200 --> 00:06:38,890 we'd have two failing test cases. 131 00:06:38,890 --> 00:06:39,951 So it's important to separate it out. 132 00:06:39,951 --> 00:06:46,150 Also I'm starting to see some duplicated code that we should talk about. 133 00:06:46,150 --> 00:06:48,640 Great, we did it. 134 00:06:48,640 --> 00:06:52,570 We practiced the AAAs by Arranging, Acting and Asserting and 135 00:06:52,570 --> 00:06:55,990 followed the blank line style to separate them. 136 00:06:55,990 --> 00:06:59,191 We did some more static importing of the amazing JUnit assert. 137 00:07:00,630 --> 00:07:03,760 I also sort of forced you through the example of why you might 138 00:07:03,760 --> 00:07:06,360 not want to have double assertions in a test. 139 00:07:06,360 --> 00:07:11,100 It's usually a code smell that you should probably be writing a separate test. 140 00:07:11,100 --> 00:07:13,890 After we wrap up here, you think you could help me correct 141 00:07:13,890 --> 00:07:17,020 the problem by writing an additional test to make sure that things are emptied? 142 00:07:18,720 --> 00:07:21,650 We were definitely starting to repeat ourselves at the end, weren't we? 143 00:07:21,650 --> 00:07:24,102 This is something we want to be careful about. 144 00:07:24,102 --> 00:07:27,410 We wanna make sure that we take the same programming best practices and 145 00:07:27,410 --> 00:07:29,180 apply them to our tests. 146 00:07:29,180 --> 00:07:31,001 We want legible well thought out tests. 147 00:07:31,001 --> 00:07:33,281 Just like we want for our production company. 148 00:07:33,281 --> 00:07:37,680 The good news is JUnit has this thought out already. 149 00:07:37,680 --> 00:07:40,961 There is a way of creating shared functionality for your tests. 150 00:07:40,961 --> 00:07:43,510 Let's explore it, right after this quick break.