Setting Up a Project for Tests10:22 with Craig Dennis
Let's learn how to use IntelliJ IDEA to develop a test suite.
- Vending Machine Repo
- GitHub and Android Studio from Ben Deitch our Android teacher. Android studio is just IntelliJ IDEA in a costume.
- Managing Dependencies with Gradle workshop
- IntelliJ IDEA 14 Test template is different. If you want to have access to the static methods off of the
junit.org.Assertclass make sure you
import static junit.org.Assert.*
I got the vending machine project all fixed up and 0:00 I pushed my changes through the repository on GitHub. 0:02 I ended up breaking out those components out of the main vending machine class and 0:05 made them public facing so those components are all ready to be tested. 0:08 Let's go ahead and get the project up and running on your local machine and 0:13 then get the project all ready for tests. 0:16 I'm going to be using IntelliJ Idea community edition Version 15 so 0:19 my screen may look a little different than yours. 0:24 Now don't worry if things don't look exactly the same on your screen. 0:27 Now one of the difficulties in using software that just keeps on getting more 0:30 and more awesome is that it's hard [LAUGH] to keep up with, right? 0:34 So please check the teacher's notes for more information on the possible 0:37 differences, and I'll call them out when they're appropriate. 0:40 I do always recommend upgrading to get the latest changes for this amazing IDEA. 0:43 All right, so if you aren't comfortable yet with GitHub, 0:48 you can also get the downloads from the download sections on this video. 0:50 But, I encourage you to sharpen your GitHub skills and play along. 0:54 Then, our Android teacher produced an excellent workshop on using GitHub and 0:59 Android studio, and it works basically the same as IntelliJ IDEA. 1:02 So let's shoot over to the repository, the link's in the teacher's notes. 1:07 I'm gonna come here to the repo and down here there's this clone URL, 1:10 I'm gonna click the Copy to clipboard button. 1:13 Gonna come back over here, I'm gonna say Check out from Version Control. 1:16 Gonna choose GitHub. 1:20 And then I'm gonna paste that URL here and I'm gonna click Clone. 1:23 Then it says you've checked out an IDEA project. 1:31 Would you like to open it? 1:34 And we definitely do. 1:35 Okay, so now I'm gonna see this message, Unregistered VCS root detected. 1:38 VCS being version control system. 1:42 So what we're gonna do here is I'm just gonna click Add root, because it's fine. 1:45 We're gonna run that locally here. 1:49 When I created this project originally I created it from a command 1:51 line application template. 1:54 So I could show things off the text as we were on the airplane, right? 1:55 So if you look at the project, if you look here under source And 1:58 then there's a program here called Main. 2:03 I'm gonna open this up and you're gonna see this question 2:05 about adding things to get, go ahead and click remember and don't ask again. 2:10 This is only the first time you only have to ask to answer this once. 2:14 So, also because we haven't, this is the first time using this project, 2:17 it's gonna ask us to set up the SDK, the Software Development Kit, right? 2:20 So let's click that. 2:24 And what we want to do is we want to choose the JDK here which is the 1.8 JDK. 2:25 And let's click okay. 2:30 So, if you remember we want to run this main program right? 2:31 Let's just take a look at it really quick. 2:34 So it makes a new vendingmachine. 2:35 And it restocks some twinkies. 2:37 It adds some money and then it vents. 2:39 Okay? And One of the things that happens is 2:43 when it vends, we wanna send off that notifications so 2:46 that's what this little notifier up here is doing. 2:48 So we should see this message, if everything goes well, 2:50 we should see this message. 2:52 So let's choose run. 2:53 And then we'll choose run. 2:56 And it's gonna make a new configuration and call it main. 2:58 Cool, so we got Restocking, Adding money, Vending, and 3:03 then here's our notification Sold items Twinkies for 75. 3:06 Awesome. 3:09 Now since JUnit is so popular in developing Java applications, 3:10 IntelliJ has made integration crazy easy. 3:14 Let's walk through how to make the magic happen. 3:17 So, junit is an external library, 3:20 meaning it's Java code that we need to import into our project. 3:23 We can get there pretty quickly by doing just this. 3:27 So let's say that we wanted to test the Creditor class first. 3:30 So let's go over here and we'll open up the Creditor. 3:34 If we right click here on the creditor and 3:36 we choose go to and you'll see that there's a test thing here. 3:38 See how popular tests are, it assumes that there's a test. 3:42 Now of course, obviously there's not one yet because we're gonna write it. 3:45 So let's choose create new test. 3:48 The style log might look a little bit different. 3:51 What we're going to choose here is we're going to choose JUnit4. 3:54 Okay, from the list. 3:59 It defaulted to the groovy J unit. 4:00 But we want JUnit4 and 4:02 when we do that you'll see that there's a little message here. 4:04 It says J unit 4 library is not found in the module. 4:07 Fix it, yeah, let's click fix. 4:10 Okay and we're gonna use JUnit4 from the IntelliJ distribution. 4:13 Okay, so now let's take a look. 4:18 So the default name is Creditor test. 4:19 That works. 4:22 Most teams define how they want to name their test fixtures. 4:23 Some teams like to call it with test as the prefix so 4:27 that all the files line up in the view, so that all the tests are grouped together. 4:30 Others make this plural sometimes as it's a fixture and 4:34 there could be several right, there's gonna multiple Creditor tests. 4:36 Its up to you and your team though. 4:39 For now let's just use the default. 4:41 So the other thing that's happening here is it's stating that the destination 4:43 should be in the same package right? 4:46 So all of the code lives in this calm team treehouse vending package. 4:48 And it's saying that shouldn't be in that same package. 4:51 And yeah it should be. 4:54 That way we can access protected and package private actions if we need to. 4:56 But remember we shouldn't but just in case they're there. 5:01 Okay and the next is saying generate test methods for and it's just showing 5:05 each of the methods that's currently in this creditor class, right? 5:09 So we have addFunds and deduct and refund. 5:13 Sad asking if it should test those. 5:15 Now this isn't the behavior that we actually want. 5:17 It's just doing its best to help us out, but it's being a little silly, right? 5:20 So let's go ahead and choose one so I can show you what it does. 5:24 So let's choose refund and then we're gonna click OK. 5:26 All right, so this is our new test. 5:31 And these templates tend to update a bit between IntelliJ versions, 5:33 especially the major releases, so 5:37 this might look a little bit different depending on your version. 5:39 But what I want you to notice here is this is just a plain old class, okay? 5:41 And there's a method here that's annotated with an error of org,junit.Test and 5:46 it's annotating the method that returns nothing and throws an exception. 5:54 This is our first test case. 5:59 Remember, it doesn't have to start with the word test and 6:01 in fact, this is kinda bad practice, right? 6:04 And it hard to comes back to the day where we used to require that naming structure. 6:06 But let's take a look at these highlighted errors here. 6:11 It seems that it doesn't know anything about the JUnit package even 6:13 though we clicked that button that said fix. 6:17 So here's a problem, there's currently a bug, we're on 15.0. 6:19 So it hasn't been fixed yet. 6:23 So by choosing File > Project Structure, 6:25 and then if we go in here into Modules and then over to Dependencies over here. 6:32 you'll see that its added this j unit four, and 6:37 it's underneath what's here is the test scope. 6:39 We can fix this by just switching this to the provided scope. 6:42 Okay, and then I'm gonna click apply, and then I'm gonna click okay. 6:47 Okay, so the errors are gone and 6:53 actually if that error wasn't in place what would happen is this. 6:55 This Replace qualified name with 'import.' It would look like this. 6:58 Let's say @Test and would say import org.junit.Test. 7:01 Now remember, 7:05 this is from the package of org.junit and that's your domain name reversed. 7:06 So the website actually is junit.org. 7:10 All right, so if we cruise over here and we look at these external libraries. 7:13 Let's open this up, you can see that there's a j unit four library here, and 7:16 if we open this up, you can see that there are two jar files that are attached there. 7:21 A jar file is essentially just a big zip file filled with our java classes. 7:26 We can actually package our code just like JUnit team did and 7:30 make libraries that other people can use. 7:34 And when you do that you typically don't wanna ship your test with it. 7:36 So a best practice that emerged is to instead of putting your test in you source 7:40 code route like we have here, right? 7:44 So here's the creditor test in the source code root. 7:46 You make a separate folder and 7:49 this separation also helps quite a bit on large code bases, 7:51 by just keeping the sheer number of files you have to look through at a minimum. 7:54 I mean, imagine this on a large code base. 7:58 There would be a whole bunch of tests after each one of those files. 8:00 You don't really want that. 8:02 So one way to solve this problem, 8:04 is to have your tests stored in a separate folder. 8:05 And IntelliJ does an awesome job of supporting this best practice for you. 8:08 Here, let me show you. 8:11 So I'm gonna come up here and I'm gonna right-click and choose new, 8:12 directory, and we're gonna call it tests. 8:17 And then if I right click the new tests directory that showed up and 8:22 choose mark directory as test sources root. 8:26 So let's go ahead and we'll just grab this original creditor test and let's kill it. 8:31 I'm just gonna press the backspace key and them I'm gonna say it's a safe delete. 8:35 Let's get rid of it and let's go ahead and let's make another one of those. 8:39 So again right-click, and choose go to Test, create new test. 8:42 And we're still gonna say it's in the same package. 8:49 Let's go ahead and we'll check, refund again, just to pop that back in there. 8:50 The destination package is the same and if we click okay. 8:53 You'll see what happens, it shows up in the test directory now. 8:58 It's not up in the source folder, it's over here in the test sources route. 9:00 Awesome right, and 9:05 also note that now that we made that fix this is working properly right? 9:05 It says at test and no with no red lines. 9:09 Oh, one thing I wanna make sure is that we change to our new coding style. 9:13 If you followed along in previous courses where we used IntelliJ you 9:17 probably have some code generation settings all set up. 9:20 So lets go ahead and lets press Cmd + comma or Ctrl + Alt + S on Windows. 9:23 And choose Editor > Code Style > Java > flip over to the Code Generation tab, 9:31 and see here where it has the name prefix of M? 9:38 We're not doing that anymore so we can get rid of that and click OK. 9:42 And, there we go. 9:48 We're all set. 9:48 And, now, next time things get generated, they will not have the im prefix, great. 9:49 Awesome because, so many people test, 9:54 you'll find that the flow is basically just built into the editor. 9:56 You'll find almost all the popular Java IDEs support this JUnit framework. 9:59 Remember, you don't need an IDE to do this at all. 10:04 You very well could download the JUnit Library and 10:08 run things yourself from the command line. 10:11 The IDE is just providing a nice wrapper around an awesome test runner. 10:12 Let's take a quick break and I'll show you how to kick off the test runner and 10:17 we'll write our first failing test. 10:20
You need to sign up for Treehouse in order to download course files.Sign up