1 00:00:00,150 --> 00:00:02,320 I got the vending machine project all fixed up and 2 00:00:02,320 --> 00:00:05,210 I pushed my changes through the repository on GitHub. 3 00:00:05,210 --> 00:00:08,780 I ended up breaking out those components out of the main vending machine class and 4 00:00:08,780 --> 00:00:12,300 made them public facing so those components are all ready to be tested. 5 00:00:13,370 --> 00:00:16,260 Let's go ahead and get the project up and running on your local machine and 6 00:00:16,260 --> 00:00:18,130 then get the project all ready for tests. 7 00:00:19,540 --> 00:00:24,640 I'm going to be using IntelliJ Idea community edition Version 15 so 8 00:00:24,640 --> 00:00:27,550 my screen may look a little different than yours. 9 00:00:27,550 --> 00:00:30,310 Now don't worry if things don't look exactly the same on your screen. 10 00:00:30,310 --> 00:00:34,380 Now one of the difficulties in using software that just keeps on getting more 11 00:00:34,380 --> 00:00:37,780 and more awesome is that it's hard [LAUGH] to keep up with, right? 12 00:00:37,780 --> 00:00:40,490 So please check the teacher's notes for more information on the possible 13 00:00:40,490 --> 00:00:43,140 differences, and I'll call them out when they're appropriate. 14 00:00:43,140 --> 00:00:48,150 I do always recommend upgrading to get the latest changes for this amazing IDEA. 15 00:00:48,150 --> 00:00:50,790 All right, so if you aren't comfortable yet with GitHub, 16 00:00:50,790 --> 00:00:54,550 you can also get the downloads from the download sections on this video. 17 00:00:54,550 --> 00:00:59,030 But, I encourage you to sharpen your GitHub skills and play along. 18 00:00:59,030 --> 00:01:02,930 Then, our Android teacher produced an excellent workshop on using GitHub and 19 00:01:02,930 --> 00:01:07,170 Android studio, and it works basically the same as IntelliJ IDEA. 20 00:01:07,170 --> 00:01:10,800 So let's shoot over to the repository, the link's in the teacher's notes. 21 00:01:10,800 --> 00:01:13,630 I'm gonna come here to the repo and down here there's this clone URL, 22 00:01:13,630 --> 00:01:15,510 I'm gonna click the Copy to clipboard button. 23 00:01:16,880 --> 00:01:20,460 Gonna come back over here, I'm gonna say Check out from Version Control. 24 00:01:20,460 --> 00:01:21,370 Gonna choose GitHub. 25 00:01:23,110 --> 00:01:27,817 And then I'm gonna paste that URL here and I'm gonna click Clone. 26 00:01:31,507 --> 00:01:34,330 Then it says you've checked out an IDEA project. 27 00:01:34,330 --> 00:01:35,340 Would you like to open it? 28 00:01:35,340 --> 00:01:38,280 And we definitely do. 29 00:01:38,280 --> 00:01:42,800 Okay, so now I'm gonna see this message, Unregistered VCS root detected. 30 00:01:42,800 --> 00:01:45,400 VCS being version control system. 31 00:01:45,400 --> 00:01:49,360 So what we're gonna do here is I'm just gonna click Add root, because it's fine. 32 00:01:49,360 --> 00:01:51,090 We're gonna run that locally here. 33 00:01:51,090 --> 00:01:54,270 When I created this project originally I created it from a command 34 00:01:54,270 --> 00:01:55,740 line application template. 35 00:01:55,740 --> 00:01:58,780 So I could show things off the text as we were on the airplane, right? 36 00:01:58,780 --> 00:02:03,030 So if you look at the project, if you look here under source And 37 00:02:03,030 --> 00:02:05,370 then there's a program here called Main. 38 00:02:05,370 --> 00:02:10,540 I'm gonna open this up and you're gonna see this question 39 00:02:10,540 --> 00:02:14,150 about adding things to get, go ahead and click remember and don't ask again. 40 00:02:14,150 --> 00:02:17,770 This is only the first time you only have to ask to answer this once. 41 00:02:17,770 --> 00:02:20,870 So, also because we haven't, this is the first time using this project, 42 00:02:20,870 --> 00:02:24,510 it's gonna ask us to set up the SDK, the Software Development Kit, right? 43 00:02:24,510 --> 00:02:25,750 So let's click that. 44 00:02:25,750 --> 00:02:29,460 And what we want to do is we want to choose the JDK here which is the 1.8 JDK. 45 00:02:30,500 --> 00:02:31,610 And let's click okay. 46 00:02:31,610 --> 00:02:34,800 So, if you remember we want to run this main program right? 47 00:02:34,800 --> 00:02:35,990 Let's just take a look at it really quick. 48 00:02:35,990 --> 00:02:37,270 So it makes a new vendingmachine. 49 00:02:37,270 --> 00:02:39,800 And it restocks some twinkies. 50 00:02:39,800 --> 00:02:43,170 It adds some money and then it vents. 51 00:02:43,170 --> 00:02:46,330 Okay? And One of the things that happens is 52 00:02:46,330 --> 00:02:48,390 when it vends, we wanna send off that notifications so 53 00:02:48,390 --> 00:02:50,110 that's what this little notifier up here is doing. 54 00:02:50,110 --> 00:02:52,410 So we should see this message, if everything goes well, 55 00:02:52,410 --> 00:02:53,280 we should see this message. 56 00:02:53,280 --> 00:02:54,330 So let's choose run. 57 00:02:56,090 --> 00:02:57,010 And then we'll choose run. 58 00:02:58,180 --> 00:03:01,457 And it's gonna make a new configuration and call it main. 59 00:03:03,437 --> 00:03:06,269 Cool, so we got Restocking, Adding money, Vending, and 60 00:03:06,269 --> 00:03:09,230 then here's our notification Sold items Twinkies for 75. 61 00:03:09,230 --> 00:03:10,790 Awesome. 62 00:03:10,790 --> 00:03:14,860 Now since JUnit is so popular in developing Java applications, 63 00:03:14,860 --> 00:03:17,620 IntelliJ has made integration crazy easy. 64 00:03:17,620 --> 00:03:20,300 Let's walk through how to make the magic happen. 65 00:03:20,300 --> 00:03:23,100 So, junit is an external library, 66 00:03:23,100 --> 00:03:26,320 meaning it's Java code that we need to import into our project. 67 00:03:27,430 --> 00:03:30,520 We can get there pretty quickly by doing just this. 68 00:03:30,520 --> 00:03:34,120 So let's say that we wanted to test the Creditor class first. 69 00:03:34,120 --> 00:03:36,730 So let's go over here and we'll open up the Creditor. 70 00:03:36,730 --> 00:03:38,330 If we right click here on the creditor and 71 00:03:38,330 --> 00:03:42,370 we choose go to and you'll see that there's a test thing here. 72 00:03:42,370 --> 00:03:45,420 See how popular tests are, it assumes that there's a test. 73 00:03:45,420 --> 00:03:48,850 Now of course, obviously there's not one yet because we're gonna write it. 74 00:03:48,850 --> 00:03:51,930 So let's choose create new test. 75 00:03:51,930 --> 00:03:54,580 The style log might look a little bit different. 76 00:03:54,580 --> 00:03:59,130 What we're going to choose here is we're going to choose JUnit4. 77 00:03:59,130 --> 00:04:00,580 Okay, from the list. 78 00:04:00,580 --> 00:04:02,120 It defaulted to the groovy J unit. 79 00:04:02,120 --> 00:04:04,250 But we want JUnit4 and 80 00:04:04,250 --> 00:04:07,370 when we do that you'll see that there's a little message here. 81 00:04:07,370 --> 00:04:10,240 It says J unit 4 library is not found in the module. 82 00:04:10,240 --> 00:04:12,060 Fix it, yeah, let's click fix. 83 00:04:13,640 --> 00:04:17,260 Okay and we're gonna use JUnit4 from the IntelliJ distribution. 84 00:04:18,740 --> 00:04:19,950 Okay, so now let's take a look. 85 00:04:19,950 --> 00:04:22,540 So the default name is Creditor test. 86 00:04:22,540 --> 00:04:23,190 That works. 87 00:04:23,190 --> 00:04:27,040 Most teams define how they want to name their test fixtures. 88 00:04:27,040 --> 00:04:30,380 Some teams like to call it with test as the prefix so 89 00:04:30,380 --> 00:04:34,027 that all the files line up in the view, so that all the tests are grouped together. 90 00:04:34,027 --> 00:04:36,460 Others make this plural sometimes as it's a fixture and 91 00:04:36,460 --> 00:04:39,990 there could be several right, there's gonna multiple Creditor tests. 92 00:04:39,990 --> 00:04:41,610 Its up to you and your team though. 93 00:04:41,610 --> 00:04:43,310 For now let's just use the default. 94 00:04:43,310 --> 00:04:46,270 So the other thing that's happening here is it's stating that the destination 95 00:04:46,270 --> 00:04:48,040 should be in the same package right? 96 00:04:48,040 --> 00:04:51,870 So all of the code lives in this calm team treehouse vending package. 97 00:04:51,870 --> 00:04:54,630 And it's saying that shouldn't be in that same package. 98 00:04:54,630 --> 00:04:56,020 And yeah it should be. 99 00:04:56,020 --> 00:05:01,500 That way we can access protected and package private actions if we need to. 100 00:05:01,500 --> 00:05:05,420 But remember we shouldn't but just in case they're there. 101 00:05:05,420 --> 00:05:09,370 Okay and the next is saying generate test methods for and it's just showing 102 00:05:09,370 --> 00:05:13,360 each of the methods that's currently in this creditor class, right? 103 00:05:13,360 --> 00:05:15,860 So we have addFunds and deduct and refund. 104 00:05:15,860 --> 00:05:17,850 Sad asking if it should test those. 105 00:05:17,850 --> 00:05:20,720 Now this isn't the behavior that we actually want. 106 00:05:20,720 --> 00:05:24,560 It's just doing its best to help us out, but it's being a little silly, right? 107 00:05:24,560 --> 00:05:26,830 So let's go ahead and choose one so I can show you what it does. 108 00:05:26,830 --> 00:05:29,730 So let's choose refund and then we're gonna click OK. 109 00:05:31,570 --> 00:05:33,610 All right, so this is our new test. 110 00:05:33,610 --> 00:05:37,750 And these templates tend to update a bit between IntelliJ versions, 111 00:05:37,750 --> 00:05:39,150 especially the major releases, so 112 00:05:39,150 --> 00:05:41,640 this might look a little bit different depending on your version. 113 00:05:41,640 --> 00:05:46,500 But what I want you to notice here is this is just a plain old class, okay? 114 00:05:46,500 --> 00:05:54,330 And there's a method here that's annotated with an error of org,junit.Test and 115 00:05:54,330 --> 00:05:59,830 it's annotating the method that returns nothing and throws an exception. 116 00:05:59,830 --> 00:06:01,830 This is our first test case. 117 00:06:01,830 --> 00:06:04,190 Remember, it doesn't have to start with the word test and 118 00:06:04,190 --> 00:06:06,870 in fact, this is kinda bad practice, right? 119 00:06:06,870 --> 00:06:11,320 And it hard to comes back to the day where we used to require that naming structure. 120 00:06:11,320 --> 00:06:13,730 But let's take a look at these highlighted errors here. 121 00:06:13,730 --> 00:06:17,430 It seems that it doesn't know anything about the JUnit package even 122 00:06:17,430 --> 00:06:19,890 though we clicked that button that said fix. 123 00:06:19,890 --> 00:06:23,770 So here's a problem, there's currently a bug, we're on 15.0. 124 00:06:23,770 --> 00:06:25,980 So it hasn't been fixed yet. 125 00:06:25,980 --> 00:06:32,320 So by choosing File > Project Structure, 126 00:06:32,320 --> 00:06:37,440 and then if we go in here into Modules and then over to Dependencies over here. 127 00:06:37,440 --> 00:06:39,540 you'll see that its added this j unit four, and 128 00:06:39,540 --> 00:06:42,610 it's underneath what's here is the test scope. 129 00:06:42,610 --> 00:06:47,550 We can fix this by just switching this to the provided scope. 130 00:06:47,550 --> 00:06:50,960 Okay, and then I'm gonna click apply, and then I'm gonna click okay. 131 00:06:53,350 --> 00:06:55,200 Okay, so the errors are gone and 132 00:06:55,200 --> 00:06:58,940 actually if that error wasn't in place what would happen is this. 133 00:06:58,940 --> 00:07:01,798 This Replace qualified name with 'import.' It would look like this. 134 00:07:01,798 --> 00:07:05,700 Let's say @Test and would say import org.junit.Test. 135 00:07:05,700 --> 00:07:06,260 Now remember, 136 00:07:06,260 --> 00:07:10,470 this is from the package of org.junit and that's your domain name reversed. 137 00:07:10,470 --> 00:07:13,462 So the website actually is junit.org. 138 00:07:13,462 --> 00:07:16,900 All right, so if we cruise over here and we look at these external libraries. 139 00:07:16,900 --> 00:07:21,020 Let's open this up, you can see that there's a j unit four library here, and 140 00:07:21,020 --> 00:07:26,310 if we open this up, you can see that there are two jar files that are attached there. 141 00:07:26,310 --> 00:07:30,810 A jar file is essentially just a big zip file filled with our java classes. 142 00:07:30,810 --> 00:07:34,160 We can actually package our code just like JUnit team did and 143 00:07:34,160 --> 00:07:36,380 make libraries that other people can use. 144 00:07:36,380 --> 00:07:40,020 And when you do that you typically don't wanna ship your test with it. 145 00:07:40,020 --> 00:07:44,850 So a best practice that emerged is to instead of putting your test in you source 146 00:07:44,850 --> 00:07:46,400 code route like we have here, right? 147 00:07:46,400 --> 00:07:49,410 So here's the creditor test in the source code root. 148 00:07:49,410 --> 00:07:51,280 You make a separate folder and 149 00:07:51,280 --> 00:07:54,990 this separation also helps quite a bit on large code bases, 150 00:07:54,990 --> 00:07:58,280 by just keeping the sheer number of files you have to look through at a minimum. 151 00:07:58,280 --> 00:08:00,330 I mean, imagine this on a large code base. 152 00:08:00,330 --> 00:08:02,940 There would be a whole bunch of tests after each one of those files. 153 00:08:02,940 --> 00:08:04,180 You don't really want that. 154 00:08:04,180 --> 00:08:05,610 So one way to solve this problem, 155 00:08:05,610 --> 00:08:08,400 is to have your tests stored in a separate folder. 156 00:08:08,400 --> 00:08:11,570 And IntelliJ does an awesome job of supporting this best practice for you. 157 00:08:11,570 --> 00:08:12,850 Here, let me show you. 158 00:08:12,850 --> 00:08:17,216 So I'm gonna come up here and I'm gonna right-click and choose new, 159 00:08:17,216 --> 00:08:20,030 directory, and we're gonna call it tests. 160 00:08:22,345 --> 00:08:26,543 And then if I right click the new tests directory that showed up and 161 00:08:26,543 --> 00:08:29,457 choose mark directory as test sources root. 162 00:08:31,726 --> 00:08:35,160 So let's go ahead and we'll just grab this original creditor test and let's kill it. 163 00:08:35,160 --> 00:08:39,660 I'm just gonna press the backspace key and them I'm gonna say it's a safe delete. 164 00:08:39,660 --> 00:08:42,450 Let's get rid of it and let's go ahead and let's make another one of those. 165 00:08:42,450 --> 00:08:49,160 So again right-click, and choose go to Test, create new test. 166 00:08:49,160 --> 00:08:50,890 And we're still gonna say it's in the same package. 167 00:08:50,890 --> 00:08:53,920 Let's go ahead and we'll check, refund again, just to pop that back in there. 168 00:08:53,920 --> 00:08:56,760 The destination package is the same and if we click okay. 169 00:08:58,240 --> 00:09:00,620 You'll see what happens, it shows up in the test directory now. 170 00:09:00,620 --> 00:09:05,020 It's not up in the source folder, it's over here in the test sources route. 171 00:09:05,020 --> 00:09:05,640 Awesome right, and 172 00:09:05,640 --> 00:09:09,660 also note that now that we made that fix this is working properly right? 173 00:09:09,660 --> 00:09:13,140 It says at test and no with no red lines. 174 00:09:13,140 --> 00:09:17,320 Oh, one thing I wanna make sure is that we change to our new coding style. 175 00:09:17,320 --> 00:09:20,700 If you followed along in previous courses where we used IntelliJ you 176 00:09:20,700 --> 00:09:23,600 probably have some code generation settings all set up. 177 00:09:23,600 --> 00:09:31,122 So lets go ahead and lets press Cmd + comma or Ctrl + Alt + S on Windows. 178 00:09:31,122 --> 00:09:38,494 And choose Editor > Code Style > Java > flip over to the Code Generation tab, 179 00:09:38,494 --> 00:09:42,910 and see here where it has the name prefix of M? 180 00:09:42,910 --> 00:09:46,360 We're not doing that anymore so we can get rid of that and click OK. 181 00:09:48,230 --> 00:09:48,940 And, there we go. 182 00:09:48,940 --> 00:09:49,730 We're all set. 183 00:09:49,730 --> 00:09:54,800 And, now, next time things get generated, they will not have the im prefix, great. 184 00:09:54,800 --> 00:09:56,870 Awesome because, so many people test, 185 00:09:56,870 --> 00:09:59,950 you'll find that the flow is basically just built into the editor. 186 00:09:59,950 --> 00:10:03,969 You'll find almost all the popular Java IDEs support this JUnit framework. 187 00:10:04,980 --> 00:10:08,510 Remember, you don't need an IDE to do this at all. 188 00:10:08,510 --> 00:10:11,110 You very well could download the JUnit Library and 189 00:10:11,110 --> 00:10:12,890 run things yourself from the command line. 190 00:10:12,890 --> 00:10:16,300 The IDE is just providing a nice wrapper around an awesome test runner. 191 00:10:17,550 --> 00:10:20,570 Let's take a quick break and I'll show you how to kick off the test runner and 192 00:10:20,570 --> 00:10:22,350 we'll write our first failing test.