1 00:00:00,000 --> 00:00:02,230 [No Audio] 2 00:00:02,230 --> 00:00:03,550 In this video, we're going to explore 3 00:00:03,550 --> 00:00:06,416 JUnit's @Before annotation, which lets us 4 00:00:06,416 --> 00:00:09,058 keep our unit test setup code dry. 5 00:00:09,058 --> 00:00:10,991 That stands for don't repeat yourself, in other 6 00:00:10,991 --> 00:00:12,814 words, we don't have the duplication of a setup 7 00:00:12,814 --> 00:00:15,296 method, and we'll see how to do that right now. 8 00:00:15,296 --> 00:00:17,732 So let's think back now and look at the unit test 9 00:00:17,732 --> 00:00:20,288 and assess the kind of unit test that it is. 10 00:00:20,288 --> 00:00:22,046 So thinking about that test we've written, because 11 00:00:22,046 --> 00:00:24,022 that's obviously a very simple test and we don't 12 00:00:24,022 --> 00:00:26,120 usually have methods which are so simple, 13 00:00:26,120 --> 00:00:28,198 they're just a simple function which is defined 14 00:00:28,198 --> 00:00:30,466 as a static method taking two integers. 15 00:00:30,466 --> 00:00:32,650 Normally we have state on objects. 16 00:00:32,650 --> 00:00:35,444 So let's rework this production class just 17 00:00:35,444 --> 00:00:37,748 for the sake of our example. And let's 18 00:00:37,748 --> 00:00:40,291 assume that it isn't a static method. In other words, 19 00:00:40,291 --> 00:00:42,591 we have to create some instance of Calculator first. 20 00:00:42,591 --> 00:00:49,770 [No Audio] 21 00:00:49,770 --> 00:00:51,276 It's like we go back to here. 22 00:00:51,276 --> 00:00:53,016 We can see this is highlighted in red. 23 00:00:53,016 --> 00:00:54,854 This is now a compilation error. 24 00:00:54,854 --> 00:00:56,995 It can't reference from a static context. 25 00:00:56,995 --> 00:00:59,837 So we could fix it by doing this new Calculator. 26 00:00:59,837 --> 00:01:02,899 However, that violates our Arrange/ Act/ Assert 27 00:01:02,899 --> 00:01:06,382 sections because the test object, the object which 28 00:01:06,382 --> 00:01:09,324 we're testing is the system on the test that 29 00:01:09,324 --> 00:01:10,982 needs to go into the arrange phase. 30 00:01:10,982 --> 00:01:12,898 So let's just put it in the arrange phase instead, 31 00:01:12,898 --> 00:01:22,973 [No Audio] 32 00:01:22,973 --> 00:01:24,739 just to keep things nice and consistent 33 00:01:24,739 --> 00:01:26,406 with the methodology. 34 00:01:26,406 --> 00:01:29,959 So now just rerun that, just to make sure 35 00:01:29,959 --> 00:01:33,542 everything still works, which it does. That's fine. 36 00:01:33,542 --> 00:01:35,792 And let's suppose we had another test. 37 00:01:35,792 --> 00:01:37,810 [No Audio] 38 00:01:37,810 --> 00:01:39,459 Just copy this one for now, 39 00:01:39,459 --> 00:01:41,592 [No Audio] 40 00:01:41,592 --> 00:01:43,810 just click testAdd 0. 41 00:01:43,810 --> 00:01:46,504 So a test when we want to, see 42 00:01:46,504 --> 00:01:48,568 what happens when we add 0 to something. 43 00:01:48,568 --> 00:01:51,196 Not the most interesting test, but we 44 00:01:51,196 --> 00:01:52,420 don't care about that for now. 45 00:01:52,420 --> 00:01:56,068 We just care about learning about how JUnit works. 46 00:01:56,068 --> 00:01:58,072 So, this should be the answer here, 47 00:01:58,072 --> 00:02:00,070 lets just make this a bit more descriptive. 48 00:02:00,070 --> 00:02:04,503 testAddPositiveNumbers, 49 00:02:04,503 --> 00:02:06,600 [No Audio] 50 00:02:06,600 --> 00:02:11,066 testAddZeroToPositive 51 00:02:11,066 --> 00:02:13,975 [No Audio] 52 00:02:13,975 --> 00:02:16,775 and rerun this now, if I do run CalculatorTest, 53 00:02:16,775 --> 00:02:18,942 [No Audio] 54 00:02:18,942 --> 00:02:20,075 you'll now see there are two 55 00:02:20,075 --> 00:02:22,191 [No Audio] 56 00:02:22,191 --> 00:02:23,448 tests which have been run here. 57 00:02:23,448 --> 00:02:26,040 So now there are two test cases on this 58 00:02:26,040 --> 00:02:28,932 test class, and every time you click a test 59 00:02:28,932 --> 00:02:31,524 method, any output related to that test method would 60 00:02:31,524 --> 00:02:34,034 appear here, or any test failure. 61 00:02:34,034 --> 00:02:35,736 For example, assertions not passing that kind 62 00:02:35,736 --> 00:02:36,960 of thing would also appear here. 63 00:02:36,960 --> 00:02:39,972 If had a full set of test methods, say 20 64 00:02:39,972 --> 00:02:42,386 test methods here and say like three of them failed, 65 00:02:42,386 --> 00:02:45,062 then you'll be able to click on the individual failures 66 00:02:45,062 --> 00:02:48,745 and see what happened in the right hand pane here. 67 00:02:48,745 --> 00:02:50,544 But what I want to show you now and 68 00:02:50,544 --> 00:02:52,378 highlight is the fact that we have some duplication. 69 00:02:52,378 --> 00:02:54,331 [No Audio] 70 00:02:54,331 --> 00:02:57,492 So here you can see when we're arranging, either 71 00:02:57,492 --> 00:03:00,864 of these tests, we do the instantiation of the 72 00:03:00,864 --> 00:03:03,792 system of the test in the method each time. 73 00:03:03,792 --> 00:03:06,156 So we can refactor this slightly and 74 00:03:06,156 --> 00:03:07,730 we can use another one of Junits 75 00:03:07,730 --> 00:03:10,400 annotations, which is called @Before. 76 00:03:10,400 --> 00:03:13,749 So if I now use @Before again from org.junit 77 00:03:13,749 --> 00:03:15,777 [No Audio] 78 00:03:15,777 --> 00:03:19,046 and have a method public void setup. 79 00:03:19,046 --> 00:03:22,646 This will enable me to put this setup card 80 00:03:22,646 --> 00:03:24,630 [No Audio] 81 00:03:24,630 --> 00:03:25,663 in that method, 82 00:03:25,663 --> 00:03:27,600 [No Audio] 83 00:03:27,600 --> 00:03:28,621 delete this one 84 00:03:28,621 --> 00:03:30,687 [No Audio] 85 00:03:30,687 --> 00:03:32,454 and this method will be executed 86 00:03:32,454 --> 00:03:35,400 before each test, just set up the so called test 87 00:03:35,400 --> 00:03:37,944 fixture for the test and that basically means the set 88 00:03:37,944 --> 00:03:40,116 of objects or the state if you like of the 89 00:03:40,116 --> 00:03:42,866 test class before each individual test is run. 90 00:03:42,866 --> 00:03:45,134 Now you can see here this is in red, it's 91 00:03:45,134 --> 00:03:48,562 calc in here and here and that's because we need 92 00:03:48,576 --> 00:03:50,892 to actually push this state 93 00:03:50,892 --> 00:03:52,834 [No Audio] 94 00:03:52,834 --> 00:03:54,184 business system and the test that we're going to be 95 00:03:54,184 --> 00:03:57,839 running a test for, into the test class itselfs 96 00:03:57,839 --> 00:03:59,364 so we give it a private member 97 00:03:59,364 --> 00:04:03,368 variable here, which we can call calc. 98 00:04:03,368 --> 00:04:06,268 And now if we rerun 99 00:04:06,268 --> 00:04:09,218 all the tests again they should be green, 100 00:04:09,218 --> 00:04:11,450 [No Audio] 101 00:04:11,450 --> 00:04:13,176 so this is nice this basically means that 102 00:04:13,176 --> 00:04:16,644 we've got, commonality to set up our test fixture in 103 00:04:16,644 --> 00:04:20,062 this setup method, so it's not duplicated across all of 104 00:04:20,076 --> 00:04:24,125 the tests and we have individual arrange sections 105 00:04:24,125 --> 00:04:27,067 which are very specific to each test so this is going 106 00:04:27,067 --> 00:04:30,946 into being a very nice kind of well structured test. 107 00:04:30,946 --> 00:04:34,056 Now it doesn't look like much at this stage because 108 00:04:34,056 --> 00:04:36,854 this is a very trivial example. But if you imagine 109 00:04:36,854 --> 00:04:39,468 you had a class which maybe takes a few different 110 00:04:39,468 --> 00:04:42,355 collaborators a few different objects which it needs to 111 00:04:42,355 --> 00:04:45,180 interact with, and maybe they need to be set up in 112 00:04:45,180 --> 00:04:48,410 a specific way which is common across all these tests. 113 00:04:48,410 --> 00:04:50,793 Then you can imagine it, this this method would be 114 00:04:50,793 --> 00:04:53,318 quite useful. And that's how we can keep set up test 115 00:04:53,318 --> 00:04:55,979 code dry using the @Before adaptation.