1 00:00:00,012 --> 00:00:03,830 All right, so getting it set up was super easy, right? 2 00:00:03,830 --> 00:00:06,240 Wait till you see how easy it is to run this. 3 00:00:06,240 --> 00:00:10,770 So easy, in fact, that you should get in the habit of running them all the time. 4 00:00:10,770 --> 00:00:11,890 That's kind of the concept. 5 00:00:13,060 --> 00:00:15,700 If you aren't working in an environment where you're using test 6 00:00:15,700 --> 00:00:16,580 driven development, 7 00:00:16,580 --> 00:00:21,530 or TDD, you should make sure that you test as often as you make changes. 8 00:00:21,530 --> 00:00:23,740 Tests are meant to run super fast. 9 00:00:23,740 --> 00:00:26,030 You'll be surprised how quick they are. 10 00:00:26,030 --> 00:00:27,240 Some people have it set up so 11 00:00:27,240 --> 00:00:30,800 that when you save the tests run in the background automatically. 12 00:00:30,800 --> 00:00:32,450 Let's go get you comfortable running these tests. 13 00:00:34,240 --> 00:00:35,730 >> Okay, ready? 14 00:00:35,730 --> 00:00:40,050 So here's what you do: you right click on the test directory, and 15 00:00:40,050 --> 00:00:42,030 you choose Run All Tests. 16 00:00:43,990 --> 00:00:45,070 And there we go, they ran. 17 00:00:45,070 --> 00:00:47,990 1 test passed in 1 ms. 18 00:00:47,990 --> 00:00:51,440 And when we did that, it created a new run configuration so 19 00:00:51,440 --> 00:00:52,540 you don't need to do that again. 20 00:00:52,540 --> 00:00:55,520 You can just press the Play button here, right? 21 00:00:55,520 --> 00:00:59,590 You can also press the hot key Ctrl+R. 22 00:00:59,590 --> 00:01:02,050 Okay, so over here it shows you what tests run. 23 00:01:02,050 --> 00:01:05,000 Right here is the test fixture, CreditorTest and 24 00:01:05,000 --> 00:01:07,930 here is the test method, testRefund. 25 00:01:07,930 --> 00:01:09,350 And when they pass they turn green. 26 00:01:10,440 --> 00:01:12,000 So wait a second, why did that pass? 27 00:01:12,000 --> 00:01:13,770 It didn't do anything. 28 00:01:13,770 --> 00:01:15,640 Well that's something important to realize. 29 00:01:15,640 --> 00:01:19,130 If no errors happen in the method, it's assumed to pass. 30 00:01:19,130 --> 00:01:21,570 Here, let me show you how to force it to fail. 31 00:01:21,570 --> 00:01:23,540 It's actually a method called fail. 32 00:01:25,800 --> 00:01:27,200 So where is that coming from? 33 00:01:27,200 --> 00:01:32,220 That's coming from this import line here, this org.junit.Assert.* But, 34 00:01:32,220 --> 00:01:34,890 if you notice something different there's an import static here, 35 00:01:34,890 --> 00:01:36,800 there's a different keyword here. 36 00:01:36,800 --> 00:01:39,980 So, this is a pattern that's kinda handy. 37 00:01:39,980 --> 00:01:43,100 If you have a class that has a bunch of static methods 38 00:01:43,100 --> 00:01:46,580 on it as this asset class does, you can do import static. 39 00:01:46,580 --> 00:01:49,490 And then instead of having to type assert.fail, 40 00:01:49,490 --> 00:01:53,730 which is the static method off of the assert class, you can just type fail. 41 00:01:54,850 --> 00:01:58,650 And it turns out that they are a bunch of static methods on the Assert class. 42 00:01:58,650 --> 00:02:01,570 And this is the common practice to do in junit 43 00:02:01,570 --> 00:02:05,750 is to expose this in your name space so that you can use them quickly and easily. 44 00:02:05,750 --> 00:02:07,960 And check the teacher's notes for more information. 45 00:02:07,960 --> 00:02:08,490 All right. 46 00:02:08,490 --> 00:02:12,290 So now that we have a call to this static method fail of the Assert class, 47 00:02:12,290 --> 00:02:13,860 it should fail, let's make sure. 48 00:02:16,400 --> 00:02:17,320 Boom. That's a red. 49 00:02:17,320 --> 00:02:18,960 That's a gross color to have, right? 50 00:02:18,960 --> 00:02:20,890 One test failed. 51 00:02:20,890 --> 00:02:23,840 And if you look over here it shows you where it failed at. 52 00:02:23,840 --> 00:02:24,750 The exclamation point. 53 00:02:24,750 --> 00:02:27,840 Now if there where more tests, we'll see, well let's just make a new one right. 54 00:02:27,840 --> 00:02:29,330 So if we can use code generation to help us. 55 00:02:29,330 --> 00:02:33,830 So if we do command N and choose generate test method. 56 00:02:33,830 --> 00:02:35,140 It's going to ask us to change the name. 57 00:02:35,140 --> 00:02:38,270 We can just call it example, for now. 58 00:02:38,270 --> 00:02:38,830 Let's do that. 59 00:02:38,830 --> 00:02:40,220 And let's run those tests again. 60 00:02:40,220 --> 00:02:45,020 And you'll see now that the fixtures mark as failing. 61 00:02:45,020 --> 00:02:48,140 But one of them passed, then one of them failed. 62 00:02:48,140 --> 00:02:53,440 If you see down here to there is a one passed one failed, as well. 63 00:02:53,440 --> 00:02:56,640 I'll tell you how many paths I have, how many times it ran here and 64 00:02:56,640 --> 00:02:58,250 how many times it failed. 65 00:02:58,250 --> 00:03:03,140 No no, when this method was created it was generated with the test prefix. 66 00:03:03,140 --> 00:03:06,930 Again this is totally one of those old styles that's no longer needed. 67 00:03:06,930 --> 00:03:10,460 What you want it to do is define what it is that you're testing and 68 00:03:10,460 --> 00:03:12,690 what it is that you're expecting. 69 00:03:12,690 --> 00:03:16,580 So what do you say we make the test verify that when you add money, 70 00:03:16,580 --> 00:03:19,210 available funds are updated? 71 00:03:19,210 --> 00:03:20,337 Shall we? 72 00:03:20,337 --> 00:03:24,341 So, let's rename this test to 73 00:03:24,341 --> 00:03:30,045 addingFundsIncrementsAvailableFunds. 74 00:03:30,045 --> 00:03:31,600 It's pretty clear, right? 75 00:03:31,600 --> 00:03:32,960 Now, if we saw this failing, 76 00:03:32,960 --> 00:03:35,268 we'd know right from the title what was going on, right? 77 00:03:35,268 --> 00:03:39,770 We know uh-oh, when we add funds it's not incrementing the amount available. 78 00:03:39,770 --> 00:03:42,150 Pretty clear, right, if we name it properly. 79 00:03:42,150 --> 00:03:45,400 So we need to write the body of this method to make sure that it happens. 80 00:03:45,400 --> 00:03:48,030 So first we'll set up the environment, then we'll add the money, and 81 00:03:48,030 --> 00:03:53,380 then finally we'll check or assert that available funds were changed. 82 00:03:53,380 --> 00:03:55,420 Let's learn how to write that test write after this quick break.